#C14325. Spiral Matrix Traversal

    ID: 43962 Type: Default 1000ms 256MiB

Spiral Matrix Traversal

Spiral Matrix Traversal

Given an \( m \times n \) matrix, your task is to output the elements of the matrix in spiral order. Starting from the top-left corner, proceed right, then down, then left, and then up, continuing in this spiral pattern until all elements have been visited. 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 begins with a line containing two integers, \( m \) and \( n \) (the number of rows and columns respectively). If \( m > 0 \), the next \( m \) lines each contain \( n \) space-separated integers representing the matrix's rows.

outputFormat

Output a single line containing 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