

For example:
Input Matrix: [ [ 1, 2, 3 ]
[ 4, 5, 6 ]
[ 7, 8, 9 ] ]
Output Matrix: [ [ 4, 1, 2 ]
[ 7, 5, 3 ]
[ 8, 9, 6 ] ]
The output matrix is generated by rotating the elements of the input matrix in a clockwise direction. Note that every element is rotated only once.
You do not need to print anything; it has already been taken care of. Also, update the given matrix in-place.
The first line of input contains an integer 'T' representing the number of test cases. Then the test cases follow.
The first line of each test case contains two single-spaced integers N and M, representing the number of rows and columns of the matrix, respectively.
The next N line contains M single-spaced integers denoting the matrix elements.
For each test case, the modified matrix is printed.
The output for each test case is in a separate line.
1 <= T <= 10
1 <= N, M <= 100
-10^5 <= data <= 10^5,
where ‘T’ is the number of test cases, ‘N’ and ‘M’ are the numbers of rows and columns respectively and ‘data’ is the value of the elements of the matrix.
The idea is to consider the matrix in the form of rings and then rotate each ring recursively. One ring will be rotated in one recursive call.
An image showing all the rings in a matrix is given below:
There are two rings in the above matrix. The outer ring is shown in the yellow colour, and the inner ring is shown in the blue colour. It’s easy to rotate the elements in the form of rings.
Matrix after rotating the outer ring:
Matrix after rotating the inner ring:
As there is no more ring, this is the modified output matrix.
Algorithm:
The idea is the same as used in the previous algorithm. We will consider the given matrix in the form of rings/squares, and this time, we will rotate the matrix in an iterative manner.
Algorithm: