
The below picture can clearly show how to traverse a matrix in spiral form.

The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains three space-separated integers ‘N’, ’M’ and ‘K’, where ‘N’ denotes the number of rows in the matrix, ‘M’ denotes the number of columns in the matrix and ‘K’ denotes the position of an element in spiral form matrix.
The next ‘N’ lines for each test case contain the ‘M’ space-separated integer of the “Nth” row of the matrix.
For each test case, return an integer that is present at the “kth” position while traversing the matrix in spiral form.
You do not need to print anything; it has already been taken care of. Just implement the given functions.
1 <= T <= 100
1 <= N * M <= 5 * 10^3
1 <= K <= N * M
-10^9 <= mat[i][j] <= 10^9
Time limit: 1 second
The basic idea is to start traversing the matrix in spiral form and find the “k’th” element in the traversal.
If the size of the matrix is ‘n*m’ then the first cycle will contain the first ‘2 * n + 2 * m - 2 * 2’ element because the first row contains ‘m’ element, the last column contains ‘n - 1’, the last row contains 'm - 1’ and the first column contains ‘n - 2’.