class Solution {
public int largest1BorderedSquare(int[][] grid) {
int ans = 0;
int n = grid.length;
int m = grid[0].length;
int[][] ud = new int[n][m];
int[][] lr = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0) // 进行数据处理,左右连续或者上下连续的一串将在grid保存
ud[i][j] = (grid[i][j] == 1 ? 1 : 0);
else
ud[i][j] = (grid[i][j] == 1 ? 1 + ud[i - 1][j] : 0);
if (j == 0)
lr[i][j] = (grid[i][j] == 1 ? 1 : 0);
else
lr[i][j] = (grid[i][j] == 1 ? 1 + lr[i][j - 1] : 0);
for (int k = 1; k <= Math.min(i, j) + 1; k++) {
if (lr[i][j] < k)
continue;
if (ud[i][j] < k)
continue;
if (lr[i - k + 1][j] = k 都能将k进行判断
continue; //continue 也是恰到好处
if (ud[i][j - k + 1] < k)
continue;
ans = Math.max(ans, k);
}
}
}
return ans * ans;
}
}