Java Solution DP
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        int m = grid.length;\n        int n = grid[0].length;\n        int[][] left_to_right = new int[m + 1][n + 1];\n        int[][] top_to_bottom = new int[m + 1][n + 1];\n        int result = 0;\n        for(int i = 0; i < m; i++){\n            for(int j = 0; j < n; j++){\n                if(grid[i][j] == 1){\n                    left_to_right[i + 1][j + 1] = left_to_right[i + 1][j] + 1;\n                    top_to_bottom[i + 1][j + 1] = top_to_bottom[i][j + 1] + 1;\n                    int length = Math.min(left_to_right[i + 1][j + 1], top_to_bottom[i + 1][j + 1]);\n                    for(; length >= 1; length--){\n                        if(left_to_right[i + 2 - length][j + 1] >= length && top_to_bottom[i + 1][j + 2 - length] >= length){\n                            result = Math.max(length, result);\n                            break;\n                        }\n                    } \n                }\n            }\n        }\n        return result * result;\n    }\n}\n```
['Java']