#K39757. Spiral Matrix Traversal

    ID: 26491 Type: Default 1000ms 256MiB

Spiral Matrix Traversal

Spiral Matrix Traversal

Given an $m \times n$ matrix, output its elements in spiral order starting from the top-left corner and moving clockwise. In a spiral traversal, the elements in the outer layer are printed first, and then the process is repeated for the remaining inner submatrix.

For example, consider 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 first line contains two integers, $m$ and $n$, representing the number of rows and columns, respectively. This is followed by $m$ lines, each containing $n$ space-separated integers representing the matrix elements.

outputFormat

Output a single line with the elements of the matrix in spiral order, separated by a single space.

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