#K47062. Spiral Matrix Traversal

    ID: 28115 Type: Default 1000ms 256MiB

Spiral Matrix Traversal

Spiral Matrix Traversal

Given an \(m \times n\) matrix, your task is to traverse the matrix in spiral order (clockwise) starting from the top-left element, and print the elements in a single line separated by spaces.

The spiral order means that you first traverse the top row from left to right, then the rightmost column from top to bottom, followed by the bottom row from right to left, and finally the leftmost column from bottom to top, then repeat the process for the inner layers.

inputFormat

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

outputFormat

Output a single line containing the elements of the matrix in spiral order, separated by spaces. If the matrix is empty, output nothing.

## sample
0 0