✔ Python3 Solution | O(n * m * min(n, m))
Complexity\n- Time complexity: O(n * m * min(n, m))\n- Space complexity: O(n * m)\n\n# Code\n\nclass Solution:\n    def largest1BorderedSquare(self, A):\n      
# Complexity\n- Time complexity: $$O(n * m * min(n, m))$$\n- Space complexity: $$O(n * m)$$\n\n# Code\n```\nclass Solution:\n    def largest1BorderedSquare(self, A):\n        n, m, ans = len(A), len(A[0]), 0\n        H, V = [row[:] for row in A], [row[:] for row in A]\n        for i in range(n):\n            for j in range(m):\n                if i and H[i][j]: H[i][j] += H[i - 1][j]\n                if j and V[i][j]: V[i][j] += V[i][j - 1]\n        for i in range(n):\n            for j in range(m):\n                for k in range(min(H[i][j], V[i][j]), ans, -1):\n                    if V[i - k + 1][j] >= k and H[i][j - k + 1] >= k:\n                        ans = k\n                        break\n        return ans * ans\n```
['Matrix', 'Python3']