You can do this in linear time.
Claim: I can build a data structure in linear time that lets me check, in constant time, whether an arbitrary rectangle is full of 1's.
Proof: Partial sums; take S[i][j]
to be the total number of 1's above and to the left of (i, j)
. The number of ones in the rectangle between (a,b)
and (c,d)
, provided (a,b)
is above and left of (c,d)
, is S[c][d] + S[a][b] - S[a][d] - S[b][c]
.
Now it's a simple scan over the array:
size = 1;
For i = 0 to m-size {
For j = 0 to n-size {
If S[i+size][j+size] - S[i][j+size] - S[i+size][j] + S[i][j] == size*size {
size++; j--; continue;
}
}
}
At the end, size
is one greater than the largest 1-full square.