Java DP Solution with Videos Explained
English\n\n\n# Chinese\n\n\n# Code\njava\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        if (grid == null || grid.length == 0)
# English\n<iframe width="560" height="315" src="https://www.youtube.com/embed/MC41ZqLqhWU" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>\n\n# Chinese\n<iframe width="560" height="315" src="https://www.youtube.com/embed/7IkOZOwc-Mc" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>\n\n# Code\n```java\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        if (grid == null || grid.length == 0) return 0;\n        int m = grid.length, n = grid[0].length;\n        int[][][] dp = new int[m + 1][n + 1][2];\n        int max = 0;\n\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; 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 len = Math.min(dp[i + 1][j + 1][0], dp[i + 1][j + 1][1]);\n                    for (int k = len; k > max; k--) {\n                        int len1 = Math.min(dp[i + 1 - k + 1][j + 1][1], dp[i + 1][j + 1 - k + 1][0]);\n                        if (len1 >= 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```
['Dynamic Programming', 'Java']