publicintmaxSquare(int[][] matrix) {if (matrix ==null||matrix.length==0|| matrix[0].length==0) {return0; }int max =0;int n =matrix.length;int m = matrix[0].length;int[][] dp =newint[n][m];// init first colfor (int i =0; i < n; i++) { dp[i][0] = matrix[i][0]; max =Math.max(max, dp[i][0]);// for case ["1"] }// init first rowfor (int j =0; j < m; j++) { dp[0][j] = matrix[0][j]; max =Math.max(max, dp[0][j]);// for case ["1"] }// cal the rest of dp matrixfor (int i =1; i < n; i++) {for (int j =1; j < m; j++) {if (matrix[i][j] >0) { dp[i][j] =Math.min(dp[i -1][j -1],Math.min(dp[i -1][j], dp[i][j -1])) +1; max =Math.max(max, dp[i][j]); } else { dp[i][j] =0; } } }return max * max;}