Java DP Solution
\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        int m = grid.length, n = grid[0].length;\n        int[][][] dp = new int[m + 
```\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        int m = grid.length, n = grid[0].length;\n        int[][][] dp = new int[m + 1][n + 1][2];\n        int max = 0;\n        for (int i = 1; i <= m; i++) {\n            for (int j = 1; j <= n; j++) {\n                if (grid[i - 1][j - 1] == 1) {\n                    dp[i][j][0] = dp[i][j - 1][0] + 1;\n                    dp[i][j][1] = dp[i - 1][j][1] + 1;\n                    int side = Math.min(dp[i][j][0], dp[i][j][1]);\n                    \n                    for (int k = side; k > 0; k--) {\n                        if (dp[i - k + 1][j][0] >= k && dp[i][j - k + 1][1] >= k) {\n                            max = Math.max(max, k);\n                        }\n                    }\n                }\n            }\n        }\n        return max * max;\n    }\n}\n```
[]