Given a matrix with ‘N’ rows and ‘M’ columns and an integer ‘K’. Your task is to find the “Kth” element which is obtained while traversing the matrix in spiral form.
Spiral Traversing in the matrix:
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.
Output Format
For each test case, return an integer that is present at the “kth” position while traversing the matrix in spiral form.
Note:
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
2
3 4 8
1 2 3 4
5 6 7 8
7 9 2 1
4 4 10
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
9
13
Test Case 1:
Given matrix is:

Spiral form traversal of given matrix is -
1=> 2=> 3=> 4=> 8=> 1=> 2=> 9=> 7 => 5=> 6 => 7
Hence at the 8'th position element is ‘9’ in spiral form traversal of the given matrix so return the integer ‘9’.
Test Case 2:
Given matrix is
Spiral form traversal of given matrix is -
1=> 2=> 3=> 4=> 8=> 12=> 16=> 15=> 14=> 13=> 9=> 5=> 6=> 7=> 11=>10
Hence at the 10'th position element is ‘13’ in spiral form traversal of the given matrix so return the integer ‘13’.
2
2 3 6
2 3 1
2 1 5
4 2 7
1 2
3 2
8 3
3 4
2
8
Traverse the whole matrix in spiral form.
The basic idea is to start traversing the matrix in spiral form and find the “k’th” element in the traversal.
O(N * M), Where ‘N’ is the number of rows and ‘M’ is the number of columns.
We are iterating every element of the matrix.
O(1)
We are using constant space.