Java DP Solution
\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        //Sanity check\n        if (grid == null || grid.length < 1 || grid[0].length
```\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        //Sanity check\n        if (grid == null || grid.length < 1 || grid[0].length < 1) {\n            return 0;\n        }\n        int rows = grid.length;\n        int cols = grid[0].length;\n        int[][] rightToLeft = new int[rows][cols];\n        int[][] bottomToTop = new int[rows][cols];\n        int max = 0;\n        //Pre-processing\n        for (int i = 0; i < rows; i++) {\n            rightToLeft[i][cols - 1] = grid[i][cols - 1];\n        }\n        for (int i = 0; i < rows; i++) {\n            for (int j = cols - 2; j >= 0; j--) {\n                if (grid[i][j] == 0) {\n                    rightToLeft[i][j] = 0;\n                } else{\n                    rightToLeft[i][j] = rightToLeft[i][j + 1] + 1;\n                }\n            }\n        }\n        for (int j = 0; j < cols; j++) {\n            bottomToTop[rows - 1][j] = grid[rows - 1][j];\n        }\n        for (int i = rows - 2; i >= 0; i--) {\n            for (int j = 0; j < cols; j++) {\n                if (grid[i][j] == 0) {\n                    bottomToTop[i][j] = 0;\n                } else {\n                    bottomToTop[i][j] = bottomToTop[i + 1][j] + 1;\n                }\n            }\n        }\n        //Iterate each possible solution\n        for (int i = 0; i < rows; i++) {\n            for (int j = 0; j < cols; j++) {\n                int n = Math.min(rightToLeft[i][j], bottomToTop[i][j]);\n                for(int k = n; k > 0; k--) {\n                    if (rightToLeft[i + k - 1][j] >= k && bottomToTop[i][j + k - 1] >= k){\n                        max = Math.max(max, k);\n                        break;\n                    }\n                }\n            }\n        }\n        return max * max;\n    }\n}\n```
[]