Micro and Matrix

4.6

7 votes
Problem

In Micro's class, students sit in a matrix of N rows and N columns, both numbered from 1 to N. Each student is having an I.Q. level, which is an integer value. It's history class right now and Micro is getting really bored. So, he started wondering about an interesting problem. He wants to find out the largest number X, such that there is a submatrix of size X×X in which all students have same I.Q. level. Please help Micro to find out the answer.

Input:
First line consists of a single integer T denoting the number of test cases.
First line of each test case consists of a single integer denoting N.
Each of the following N lines consists of N space separated integers. jth integer in ith row denotes the I.Q. level of the student sitting on jth chair of ith row.

Output:
For each test case print the largest possible value of X in a new line.

Constraints:
1T10
1N1000
1A[i][j]109,A[i][j]= I.Q. level of the student sitting on jth chair of ith row.

Note: Please use fast I/O methods.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

Biggest square submatrix with all elements having equal value is the one with top-left corner (1,1) and bottom-right corner (2,2).

Editor Image

?