2
public int largest1BorderedSquare(int[][] grid) {4
int n = grid[0].length;5
// rows[r][c] is the length of the line ended at [r,c] on row r6
int[][] rows = new int[m][n];7
// the length of the line ended at [r,c] on colume c8
int[][] cols = new int[m][n];10
for (int r = 0; r < m; r++) {11
for (int c = 0; c < n; c++) {12
if (grid[r][c] == 0) {16
rows[r][c] = c == 0 ? 1 : rows[r][c - 1] + 1;17
cols[r][c] = r == 0 ? 1 : cols[r - 1][c] + 1;18
if (res >= rows[r][c] || res >= cols[r][c]) {21
res = Math.max(res, getD(rows, cols, r, c));28
// get the dimension of the largest square which bottom-right point is [row,col]29
private int getD(int[][] rows, int[][] cols, int row, int col) {30
int len = Math.min(rows[row][col], cols[row][col]);31
for (int i = len - 1; i >= 0; i--) {32
if (rows[row - i][col] > i && cols[row][col - i] > i) {