Ninja got a moody friend who is very curious about colors. His friend presented him with a design made of various colors and gave him a stamp box. The various colors are given in the form of numbers. He can adjust the dimensions of the stamp box as per need.
Ninja is supposed to draw the given design under the given conditions:
1. Ninja can use one color at a time.
2. Ninja can’t use one color more than once.
3. The stamp box is in the form of a rectangle. So, Ninja can only draw rectangles(or squares) by adjusting the dimensions of the stamp box.
4. Once Ninja prints any color using the stamp box, the old color won’t be visible and only the new color will be visible.
Ninja is very busy in doing some other stuff but he can find some time if it is possible to print the design under the given conditions and if it is impossible to print the design, he won’t waste time on it.
Can you help Ninja to find if it is possible to print the design or not?
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first line contains two space-separated integers ‘N’ and ‘M’, which denotes the number of rows and columns in the matrix ‘MAT’.
The next ‘N’ lines contain ‘M’ space-separated integers denoting the colors at that point.
Output Format:
For each test case, print 1 if it is possible to draw the given design. Otherwise, print 0.
Print the output of each test case in a separate line.
Note:
You are not required to print the expected output, it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= N <= 100
1 <= M <= 100
1 <= MAT[i][j] <= 50
Where 'MAT[i][j]' is the color in the coordinate (i, j).
Time Limit: 1 sec
2
3 3
1 2 3
4 5 6
7 3 7
4 4
1 3 2 2
1 3 2 2
1 1 1 1
1 4 4 4
0
1
In the first test case, there are 7 colors in a matrix of size 3X3 as shown in the figure.

To create the given design, we need to reuse one of the two colors - 3 or 7. Otherwise, it is not possible to make the given design under the given conditions. So, the output is 0.
In the second test case, there are 4 colors in a matrix of size 4X3 as shown in the figure.

To create the given design,
One of the options to make the given design is:
Make the stamp box of size 3X3 and print color 1 in all the boxes.
Make the stamp box of size 2X2 and then print color 2 in the top right corner.
Make the stamp box of size 2X1 and then print color 3 beside color 2.
Make the stamp box of size 1X3 and then print color 1 behind color 2.
Hence, it is possible to make this design. So, the output is 1.
2
4 3
1 2 1
1 3 1
1 1 3
1 1 1
2 2
1 2
3 4
0
1
In the first test case, it is possible to draw the given design. Thus, we will print 1 as output.
In the second test case, it is impossible to draw the design. So, we will print 0.
Your task is to fill the colors to make the design. Can you do the reverse?
Approach: The idea is to start removing the colors one by one instead of filling the colors.
After making the design, if you can successfully remove the colors, then it will be a blank grid. If it is not possible to make the design under the given conditions, then you won’t be able to reach a blank sheet.
Let’s say if we made a rectangle of 3X3 using color 1 and inside that, we made a rectangle of size 2X2 using color 2. How can we store the information that we have to remove color 2 first and then color 1?
We will use topological sorting for this. We will store indegrees of every color. For the above example, the indegree of color 2 will be 0, and the indegree of color 1 will be 1. It implies that we will remove all the colors with indegrees 0, and then reduce the indegrees of all the connected colors to it. This is similar to topological sorting.
The overview is: Find the colors that are present in the matrix -> find the rectangle in which color lies for all the given colors -> find indegrees of all the colors -> perform topological sorting.
The steps are as follows:
O(N*M), where N and M denote the number of rows and columns in the matrix ‘MAT’.
As we are traversing through all the elements of the matrix ‘MAT’. Meanwhile, we are also inserting the colors in a hashmap or hash set which takes constant time. Hence, the overall time complexity is O(N*M).
O(N*M), where N and M denote the number of rows and columns in the matrix ‘MAT’.
As we are storing all the colors, their dependencies, etc. In the worst case, all colors will be different and we need to store all the colors. Hence, the space complexity is O(N*M).