Ninja has been given two integer matrices ‘MAT1’ and ‘MAT2’ of sizes 'N1 x M1' and 'N2 x M2', respectively. He is also given a magic sword. Using this sword, he can cut a sub-square of any size from the given matrices if it is present in both the matrices. Formally, he can cut that sub-square from the first matrix which is also present in the other matrix with the same values inside both sub-squares.
However, Ninja can use this magic sword only once. So, he decides to cut that common sub-square which has the largest size.
For example:
For the matrices 'MAT1' and 'MAT2' as shown in the image below, the largest sub-square which is common in both of the matrices is of size 2.

Can you help Ninja achieve this task?
Note:
If there is no such sub-square present then return 0.
The first line of input contains an integer 'T’ denoting the number of test cases. Then each test case follows.
The first line of each test case contains two single space-separated integers ‘N1’ and ‘M1’ representing the size of the matrix ‘MAT1’.
The next ‘N1’ lines of each test case contain ‘M1’ single space-separated integers denoting the values of ‘MAT1’.
The next line of each test case contains two single space-separated integers ‘N2’ and ‘M2’ representing the size of the matrix ‘MAT2’.
The next ‘N2’ lines of each test case contain ‘M2’ single space-separated integers denoting the values of ‘MAT2’.
Output Format :
For each test case, print a single line containing a single integer denoting the size of the largest possible sub-square which is also present in another matrix.
The output of each test case will be printed 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’ <= 100
1 <= ‘N1, ‘M1’, ‘N1’, ‘M2’ <= 50
1 <= ‘MAT1[ i ][ j ]’, ‘MAT2[ i ][ j ]’ <=10000
Time limit: 1 sec.
2
3 3
0 1 1
1 1 1
0 1 1
3 3
1 0 1
1 1 1
0 1 1
2 2
1 2
3 4
2 2
5 6
7 8
2
0
For sample test case 1:
The largest sub-square which is common in both of the matrices is of size 2 as shown below.

For sample test case 2:
There is no such sub-square that is common in both the matrices, so our answer is 0.

2
2 2
1 2
1 2
2 2
1 2
1 2
2 3
1 2 3
2 1 3
3 2
2 3
1 3
1 2
2
2
For sample test case 1:
In this sample test case, both matrices are equal. So largest sub-square which is common in both of the matrices is of size 2.

For sample test case 2:
The largest sub-square which is common in both of the matrices is of size 2 as shown below.

Can we use Rabin Karp algorithm?
Hashing will allow us to represent the whole sub-square as a single number. Then for comparing the sub-square present in both the matrices, we just compare the corresponding numbers. We make hash matrices ‘HashMatA’ and ‘HashMatB’ corresponding to the input matrices ‘MAT1’ and ‘MAT2’. We use the idea of the Rabin-karp pattern matching algorithm and modify it for calculating the hash value which is a single number representing 2-D matrices.
Here is the algorithm :
O(log(N1) * (N2 * M2)) where ‘N1’ denotes the number of rows of the matrix ‘MAT1’ and ‘N2’ and ‘M2’ denotes the number of rows and columns of the matrix ‘MAT2’.
To find the maximum size of the sub-square we are applying binary search. We traverse in the ‘MAT1’ and push all its values into ‘hashValueOfMat1’. Then we traverse the other matrix ‘MAT2’ and find if the hash value of the given size of the sub-square is present in ‘hashValueOfMat1’ or not. Hence, the overall time complexity is O(log(N1) * (N2 * M2)).
O(N1 * M1) + O(N2 * M2) Where ‘N1’ and ‘M1’ denote the number of rows and columns of the matrix ‘MAT1’ and ‘N2’ and ‘M2’ denotes the number of rows and columns of the matrix ‘MAT2’.
We are using extra matrices of sizes ‘N1 * M1’ and ‘N2 * M2’ to store the hash values of ‘MAT1’ and ‘MAT2’ respectively. We also use the set ‘hashValueOfMat1’ to store values of ‘MAT1’ i.e O(N1 * M1). In the worst-case i.e when the size of sub-square is 1, then we are pushing hash value for all elements of ‘MAT1’. Hence the overall space complexity is O(N1 * M1) + O(N2 * M2).