Clean Python | High Speed | O(n) time, O(1) space | Beats 98.9%
\n\n# Code\n\nclass Solution:\n    def largest1BorderedSquare(self, A):\n        m, n = len(A), len(A[0])\n        res = 0\n        top, left = [a[:] for a in A
\n\n# Code\n```\nclass Solution:\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 range(m):\n            for j in range(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 range(min(m, n), 0, -1):\n            for i in range(m - r + 1):\n                for j in range(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```
['Python3']