#K41417. Reverse Spiral Order Traversal

    ID: 26861 Type: Default 1000ms 256MiB

Reverse Spiral Order Traversal

Reverse Spiral Order Traversal

You are given a matrix of integers with m rows and n columns. Your task is to output the elements of the matrix in a "reverse spiral" order. In a reverse spiral traversal, you start from the bottom-right corner and traverse the outer layer of the matrix in the following order:

  • Traverse the bottom row from right to left.
  • Traverse the left column from bottom to top.
  • If any rows remain, traverse the top row from left to right.
  • If any columns remain, traverse the right column from top to bottom.

Continue the process with the remaining submatrix and output all elements in the order visited, separated by spaces.

Note: If the matrix is empty, print nothing.

inputFormat

The first line of input contains two integers m and n separated by a space, representing the number of rows and columns of the matrix.

Each of the next m lines contains n space-separated integers representing one row of the matrix.

outputFormat

Output a single line containing the elements of the matrix in reverse spiral order separated by a single space.

## sample
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
16 15 14 13 9 5 1 2 3 4 8 12 11 10 6 7