solution using dp+tabulation
Code\n\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        if (grid == null || grid.length == 0)\n            return 0;\n\n       
# Code\n```\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        if (grid == null || grid.length == 0)\n            return 0;\n\n        int[][][] dp = new int[grid.length + 1][grid[0].length + 1][2];\n        int max = 0;\n\n        for (int i = 0; i < grid.length; i++) {\n            for (int j = 0; j < grid[0].length; j++) {\n                if (grid[i][j] == 1) {\n                    dp[i + 1][j + 1][0] = dp[i][j + 1][0] + 1;\n                    dp[i + 1][j + 1][1] = dp[i + 1][j][1] + 1;\n\n                    int l = Math.min(dp[i + 1][j + 1][0], dp[i + 1][j + 1][1]);\n                    for (int k = l; k > max; k--) {\n                        int len = Math.min(dp[i + 1 - k + 1][j + 1][1], dp[i + 1][j + 1 - k + 1][0]);\n                        if (len >= k) {\n                            max = Math.max(max, k);\n                        }\n                    }\n                } else {\n                    dp[i + 1][j + 1][0] = 0;\n                    dp[i + 1][j + 1][1] = 0;\n                }\n            }\n        }\n\n        return max*max;\n    }\n}\n```
['Array', 'Dynamic Programming', 'Matrix', 'Java']