[Java/C++/Python] Straight Forward
Explanation\n1. Count the number of consecutive 1s on the top and on the left.\n2. From length of edge l = min(m,n) to l = 1, check if the 1-bordered square exi
## **Explanation**\n1. Count the number of consecutive 1s on the top and on the left.\n2. From length of edge `l = min(m,n)` to `l = 1`, check if the 1-bordered square exist.\n<br>\n\n## **Complexity**\nTime `O(N^3)`\nSpace `O(N^2)`\n<br>\n\n**Java:**\n```java\nclass Solution {\n    public int largest1BorderedSquare(int[][] A) {\n        int m = A.length, n = A[0].length;\n        int[][] left = new int[m][n], top = new int[m][n];\n        for (int i = 0; i < m; ++i) {\n            for (int j = 0; j < n; ++j) {\n                if (A[i][j] > 0) {\n                    left[i][j] = j > 0 ? left[i][j - 1] + 1 : 1;\n                    top[i][j] = i > 0  ? top[i - 1][j] + 1 : 1;\n                }\n            }\n        }\n        for (int l = Math.min(m, n); l > 0; --l)\n            for (int i = 0; i < m - l + 1; ++i)\n                for (int j = 0; j < n - l + 1; ++j)\n                    if (top[i + l - 1][j] >= l &&\n                            top[i + l - 1][j + l - 1] >= l &&\n                            left[i][j + l - 1] >= l &&\n                            left[i + l - 1][j + l - 1] >= l)\n                        return l * l;\n        return 0;\n    }\n}\n```\n\n**C++:**\n```cpp\n    int largest1BorderedSquare(vector<vector<int>>& A) {\n        int m = A.size(), n = A[0].size();\n        vector<vector<int>> left(m, vector<int>(n)), top(m, vector<int>(n));\n        for (int i = 0; i < m; ++i) {\n            for (int j = 0; j < n; ++j) {\n                left[i][j] = A[i][j] + (j && A[i][j] ? left[i][j - 1] : 0);\n                top[i][j] = A[i][j] + (i && A[i][j] ? top[i - 1][j] : 0);\n            }\n        }\n        for (int l = min(m, n); l > 0; --l)\n            for (int i = 0; i < m - l + 1; ++i)\n                for (int j = 0; j < n - l + 1; ++j)\n                    if (min({top[i + l - 1][j], top[i + l - 1][j + l - 1], left[i][j + l - 1], left[i + l - 1][j + l - 1]}) >= l)\n                        return l * l;\n        return 0;\n    }\n```\n\n**Python:**\n```python\n    def largest1BorderedSquare(self, A):\n        m, n = len(A), len(A[0])\n        res = 0\n        top, left = [a[:] for a in A], [a[:] for a in A]\n        for i in xrange(m):\n            for j in xrange(n):\n                if A[i][j]:\n                    if i: top[i][j] = top[i - 1][j] + 1\n                    if j: left[i][j] = left[i][j - 1] + 1\n        for r in xrange(min(m, n), 0, -1):\n            for i in xrange(m - r + 1):\n                for j in xrange(n - r + 1):\n                    if min(top[i + r - 1][j], top[i + r - 1][j + r - 1], left[i]\n                           [j + r - 1], left[i + r - 1][j + r - 1]) >= r:\n                        return r * r\n        return 0\n```\n
[]