#C6712. Spiral Matrix Traversal

    ID: 50503 Type: Default 1000ms 256MiB

Spiral Matrix Traversal

Spiral Matrix Traversal

Given a matrix of dimensions n×mn \times m, your task is to output the spiral order traversal of the matrix. The spiral order starts from the top-left element and proceeds in a clockwise spiral until every element is visited.

The traversal order is: right, down, left, and up. This problem requires you to carefully handle various cases including matrices with a single row, a single column, or even an empty matrix.

inputFormat

The first line contains two space-separated integers nn and mm (0n,m10000 \le n,m \le 1000) representing the number of rows and columns respectively. The next nn lines each contain mm space-separated integers denoting the elements of the matrix.

outputFormat

Print a single line containing the elements of the matrix in spiral order as space-separated integers. If the matrix is empty (i.e. n=0n = 0 or m=0m = 0), output nothing.## sample

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