#K60367. Spiral Matrix Traversal

    ID: 31071 Type: Default 1000ms 256MiB

Spiral Matrix Traversal

Spiral Matrix Traversal

Given a 2D matrix of integers, your task is to output its elements in spiral order. The spiral order starts from the top-left element, goes to the right, then down, then left, and finally up, continuing in this pattern until every element is traversed.

For example, given the matrix:

1 2 3
4 5 6
7 8 9

the spiral order is: 1 2 3 6 9 8 7 4 5.

inputFormat

The input is read from standard input (stdin). The first line contains two integers \(n\) and \(m\), representing the number of rows and columns of the matrix respectively. Each of the following \(n\) lines contains \(m\) space-separated integers, representing the elements of the matrix.

outputFormat

Print the elements of the matrix in spiral order in a single line, with each element separated by a single space. The output should be 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

</p>