

• Right Shift
• Left Shift
• Up Shift
• Down Shift
Let the grid MAT1 be-

After one right shift, it will look like-

After one left shift, it will look like-

After one up shift, it will look like-

After one down shift, it will look like-

The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.
The first line of each test case contains one integer ‘N’ denoting the number of rows and columns in the grid ‘MAT1’ and ‘MAT2’.
The next ‘N’ lines of each test case contain ‘N’ space-separated integers each representing the rows of ‘MAT1’
The next ‘N’ lines of each test case contain ‘N’ space-separated integers each representing the rows of ‘MAT2’.
For each test case, print the maximum possible overlap.
The output for each test case will be printed in a new line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 30
0 <= MAT1[i][j] <= 1
0 <= MAT2[i][j] <= 1
Time Limit: 1 sec.
For any cell A of MAT1 which has one and cell B of MAT2 which has one we can define linear transformation L as - (difference of X - coordinates of A and B, difference of Y - coordinates of A and B). The cells which come under the same overlapping zone will have the same value of L.
The algorithm will be-
Let there be ‘x’ right shifts and ‘y’ up shifts of MAT1. We can iterate over x and y to find the final configuration of MAT1 and calculate the value of overlap. All possible left and down shifts of MAT1 can be equivalently represented by right and up shifts of MAT2.
The algorithm will be-