class Solution {public: int maximalSquare(vector>& matrix) { int m = matrix.size(); if (m == 0) return 0; int n = matrix[0].size(); if (n == 0) return 0; vector > dp(m+1, vector (n+1,0)); int res = 0; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) { if (matrix[i-1][j-1] == '0') dp[i][j] = 0; else { dp[i][j] = min(dp[i-1][j], min(dp[i][j-1], dp[i-1][j-1])) + 1; res = max(res, dp[i][j]); } } return res * res; }};