

You are given two integers ‘N’ and ‘M’. Your task is to create a matrix of size N x M in which every element is either ‘X’ or ‘0’. The ‘X’s and ‘0’s must be filled alternatively, the matrix should have an outermost rectangle of ‘X’s, then a rectangle of ‘0’s, then a rectangle of ‘X’s, and so on.
For example :For given dimension N = 5 and M = 5 :
[ [X, X, X, X, X],
[X, 0, 0, 0, X],
[X, 0, X, 0, X],
[X, 0, 0, 0, X],
[X, X, X, X, X] ]
The first line of input contains an integer 'T' representing the number of the test case. Then the test case follows.
Each test case contains two integers ‘N’ and ‘M’ which denotes the dimensions of the matrix that you need to create.
Output Format :
For each test case, print the matrix of dimension N x M.
Note :
You do not need to print anything; It has already been taken care of. Just implement the given function.
1 ≤ T ≤ 50
1 ≤ N ≤ 100
1 ≤ M ≤ 100
Where ‘T’ is the number of test cases.
‘N’ and ‘M’ are the given dimensions.
Time Limit: 1 sec
2
3 3
2 2
X X X
X 0 X
X X X
X X
X X
For the first test case, the constructed matrix will contain two rectangles, the outermost rectangle is of ‘X’s, then there is a rectangle of ‘0’s.
For, second test case, the constructed matrix will contain one rectangle of ‘X’s.
2
4 3
5 5
X X X
X 0 X
X 0 X
X X X
X X X X X
X 0 0 0 X
X 0 X 0 X
X 0 0 0 X
X X X X X
For the first test case, the constructed matrix will contain two rectangles, the outermost rectangle is of ‘X’s, then there is a rectangle of ‘0’s.
For, second test case, the constructed matrix will contain three rectangles, where the outermost rectangle will be of ‘X’s then ‘0’s and lastly ‘X’s again.
Try to think of how you can make each rectangular frame one by one.
Our intuition is to create rectangular frames one by one such that the outermost frame consists of ‘X’s and then that of ‘0’s and again ‘X’s and so on. To implement this we need to create alternate frames of ‘X’s and ‘0’s and thereby create the whole matrix of size ‘N’ x ‘M’.
Steps are as follows:
O(N*M), where ‘N’ and ‘M’ are given dimensions to create a matrix.
Since only need a single traversal of the matrix is needed so the overall time complexity will be O(N*M).
O(N*M), where ‘N’ and ‘M’ are given dimensions to create a matrix.
We are creating a 2D matrix of size N x M, so the overall space complexity will be O(N*M).