package leetcode;

/**
 * https://leetcode.com/problems/largest-1-bordered-square/
 */
public class Problem1139 {
    public int largest1BorderedSquare(int[][] grid) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1139 prob = new Problem1139();
        System.out.println(prob.largest1BorderedSquare(new int[][]{
            {1, 1, 1},
            {1, 0, 1},
            {1, 1, 1}
        })); // 9
        System.out.println(prob.largest1BorderedSquare(new int[][]{
            {1, 1, 0, 0},
        })); // 1
        System.out.println(prob.largest1BorderedSquare(new int[][]{
            {1, 1, 1, 1},
        })); // 1
        System.out.println(prob.largest1BorderedSquare(new int[][]{
            {1, 1, 1, 1},
            {1, 1, 1, 1}
        })); // 4
        System.out.println(prob.largest1BorderedSquare(new int[][]{
            {1, 1, 1, 1},
            {1, 0, 0, 1},
            {1, 1, 1, 1},
        })); // 1
        System.out.println(prob.largest1BorderedSquare(new int[][]{
            {1, 1, 1, 1},
            {1, 0, 0, 1},
            {1, 0, 0, 1},
            {1, 1, 1, 1},
        })); // 16
    }
}

@@ -173,32 +173,951 @@
     
-// TODO%0A        return 0
+int answer = 0;%0A        int m = grid.length;%0A        int n = grid.length %3E 0 ? grid%5B0%5D.length : 0;%0A        for (int i = 0; i %3C m; i++) %7B%0A            for (int j = 0; j %3C n; j++) %7B%0A                // Make sure this is a square.%0A                for (int k = 1; k %3C= Math.min(m, n) && i + k %3C= m && j + k %3C= n; k++) %7B%0A                    if (isValid(grid, i, j, k)) %7B%0A                        answer = Math.max(answer, k * k);%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A        return answer;%0A    %7D%0A%0A    private static boolean isValid(int%5B%5D%5B%5D grid, int i, int j, int k) %7B%0A        for (int row = i; row %3C i + k; row++) %7B%0A            if (grid%5Brow%5D%5Bj%5D == 0 %7C%7C grid%5Brow%5D%5Bj + k - 1%5D == 0) %7B%0A                return false;%0A            %7D%0A        %7D%0A        for (int col = j; col %3C j + k; col++) %7B%0A            if (grid%5Bi%5D%5Bcol%5D == 0 %7C%7C grid%5Bi + k - 1%5D%5Bcol%5D == 0) %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true
 ;%0A  

Update problem 1139