#C4154. Spiral Matrix Traversal

    ID: 47661 Type: Default 1000ms 256MiB

Spiral Matrix Traversal

Spiral Matrix Traversal

Given a matrix with dimensions \(r \times c\) containing integers, your task is to perform a spiral traversal on the matrix and print the elements in the order they are visited. The traversal begins from the top-left element and proceeds in a spiral manner (right, down, left, and up) until all elements are covered.

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 first line of input contains two space-separated integers \(r\) and \(c\) representing the number of rows and columns in the matrix respectively. This is followed by \(r\) lines, each containing \(c\) space-separated integers representing the elements of the matrix.

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