Side-length DP
Time O(m × n) Space O(m × n)Pad with a zero row and column; dp[r + 1][c + 1] from the three neighbours; track the largest side.
class Solution {
public int maximalSquare(char[][] matrix) {
int m = matrix.length, n = matrix[0].length, side = 0;
int[][] dp = new int[m + 1][n + 1];
for (int r = 0; r < m; r++)
for (int c = 0; c < n; c++)
if (matrix[r][c] == '1') {
dp[r + 1][c + 1] = 1 + Math.min(dp[r][c], Math.min(dp[r][c + 1], dp[r + 1][c]));
side = Math.max(side, dp[r + 1][c + 1]);
}
return side * side;
}
}Verdict: Classic.