



The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains two single space-separated integers 'N' and 'M', denoting the number of rows and columns respectively.
The next 'N' lines, each contains 'M' single space-separated integers representing the elements in a row of the matrix.
For each test case/query, print the spiral path of the given matrix.
The output for every test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 10 ^ 2
1 <= M <= 10 ^ 2
-10 ^ 9 <= MATRIX[ i ][ j ] <= 10 ^ 9
Time Limit: 1sec.
For each boundary, we will use the 4 for loops. Now the problem is reduced to print the remaining submatrix which is handled with the help of recursion. Keep track of new dimensions(rows and columns) of the current submatrix which are passed with the help of parameters in each recursion call.
spiralPrint(matrix[][], R, C, rows, cols):
The idea is to traverse the matrix using for loops where each loop will be used for a specific direction to print all the elements in the clockwise order layer by layer. Every ‘for’ loop traverses the matrix in a single direction. The first loop traverses the matrix from left to right, the second loop traverses the matrix from top to bottom, the third traverses the matrix from the right to left, and the fourth traverses the matrix from bottom to up. After completing the traversal using 4 loops one time repeat it(4 for loops) again for the remaining submatrix. To traverse using these loops we will initialize 4 variables to keep the boundaries of the submatrix as explained below.
spiralPrint(matrix[][], rows, cols):