MaximalSquare { public static int maxSquare(char[][] matrix) { if (matrix.length == 0 || matrix[0].length == 0) return 0; int row = matrix.length, col = matrix[0].length; int[][] dp = new int[row][col]; int maxSquare = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (matrix[i][j] == '1') { if (i == 0 || j == 0) { dp[i][j] = 1; if (maxSquare < 1) { maxSquare = 1; } } else { int max = Integer.min(dp[i - 1][j - 1], Integer.min(dp[i - 1][j], dp[i][j - 1])); dp[i][j] = max + 1; if (maxSquare < dp[i][j]) { maxSquare = dp[i][j]; } } } } } return maxSquare * maxSquare; }  static int maxSquare(char[][] matrix);  }
@Test(dataProvider = "squareProvider") public void test(char[][] matrix, int expectedMaxSquare) { assertEquals(MaximalSquare.maxSquare(matrix), expectedMaxSquare); }