#K53242. Spiral Order Matrix Traversal

    ID: 29488 Type: Default 1000ms 256MiB

Spiral Order Matrix Traversal

Spiral Order Matrix Traversal

In this problem, you are given a 2D matrix of dimensions m×nm \times n. Your task is to traverse the matrix in spiral order and output the elements as they are visited. The spiral traversal starts from the top-left corner and proceeds in a clockwise manner. Specifically, you first traverse right, then down, then left, and finally up and repeat this process until every element has been visited.

Input constraints:
- The first line contains two integers, mm (number of rows) and nn (number of columns).
- The next mm lines each contain nn integers, the elements of the matrix.

Output:
- Print the matrix elements in spiral order separated by a single space.

inputFormat

The input is read from standard input (stdin). It begins with two integers mm and nn where mm is the number of rows and nn is the number of columns of the matrix. This is followed by mm lines, each containing nn space-separated integers representing the matrix.

outputFormat

Output the elements of the matrix in spiral order on a single line, separated by a space. The output is written to standard output (stdout).## sample

3 3
1 2 3
4 5 6
7 8 9
1 2 3 6 9 8 7 4 5