

The first line of input contains an integer ‘T' representing the number of test cases.
The first line of each test case contains one integer ‘N’ denoting the size of the matrix.
The next ‘N’ lines contain ‘N’ integers separated by spaces describing rows of the matrix. (each element of the matrix is either 0 or 1).
For each test case, on a separate line, output one integer - the maximum length of a side of a subsquare such that all four borders are filled with black pixels.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^3
MATRIX[i][j] = 0 or 1
Where ‘T’ is the number of test cases, ‘N’ is the size of the given array, and ‘MATRIX[i][j]’ denotes the j’th element of the i’th row of the matrix "MATRIX".
Time Limit: 1 sec
The idea is to try every possible square submatrix and check whether all the corner elements are ‘1’. A submatrix is defined by a top left corner (x1, y1) and bottom right corner (x2,y2). Since we are interested in finding a square. If we fix the bottom right corner and fix either the x or the y coordinate of the top left corner, we can calculate the other coordinate using the equation x2 - x1 = y2 - y1.
The algorithm is as follows :
Approach : The idea is to create two matrices hor[][] and ver[][]. Where hor[x][y] represents the contiguous number of 1’s, taken horizontally till the point (x, y) and ver[x][y] represents the contiguous number of 1’s, taken vertically till the point (x, y). Now, for every possible square ending at (x, y). We find the minimum of hor[x][y], ver[x][y] let’s say this maxPossibleSize. Now, the condition for row x and column y is fulfilled, we’ll definitely have a maxPossibleSize number of ones in row x and column y, in a square ending at index (x,y). Now, to check for the other two corners we can try for every possible square from k = maxPossibleSize to 0, and check if ver[x][y - k + 1] >= k and hor[x - k + 1][y] >= k, if it is update the ans accordingly.
The algorithm is as follows :