Java, brute force, 6ms
I think the code is quite straight forward, I use two helper functions to find the largest sqaure we can find at each point with some early stop conditions. I t
I think the code is quite straight forward, I use two helper functions to find the largest sqaure we can find at each point with some early stop conditions. I think there is a lot space to improve, but still this is a very naive and straight forward way to get in an interview or this contest\n```\nclass Solution {\n    int max = 0;\n    public int largest1BorderedSquare(int[][] grid) {\n        int m = grid.length, n = grid[0].length;\n        for(int i = 0; i < m; i++){\n            if(m - i < max) break;\n            for(int j = 0; j < n ; j++){\n                if(n - j < max) break;\n                if(grid[i][j] == 1){\n                    int l = getL(grid, i, j);\n                    max = Math.max(l, max);\n                }\n            }\n        }\n        return max*max; \n    }\n\t\n\t//check left and top\n    \n    private int getL(int[][] grid, int x, int y){\n        int res = 0, cur = 0;\n        for(int i = 0; x+i < grid.length && y + i < grid[0].length; i++){\n            if(grid[x+i][y] == 1 && grid[x][y+i] ==1) {\n                cur++;\n                if(isBS(grid, x, y, cur)) res = cur;\n            }\n            else break;\n        }\n        return res;\n    }\n\t\n\t//check right and bottom\n    \n    private boolean isBS(int[][] grid, int x, int y, int l){\n        for(int i = 0; i < l; i++){\n            if(grid[x+l-1][y+i] ==0 || grid[x+i][y+l-1] ==0) return false;\n        }\n        return true;\n    }\n}
[]