Share my clear python solution
\nclass Solution:\n    def largest1BorderedSquare(self, grid: List[List[int]]) -> int:\n        \n        if not grid:\n            return 0\n        \n        
```\nclass Solution:\n    def largest1BorderedSquare(self, grid: List[List[int]]) -> int:\n        \n        if not grid:\n            return 0\n        \n        res = 0\n        \n        m = len(grid)\n        n = len(grid[0])\n        \n        \n        height = [[0] * n for _ in range(m)]\n        left = [[0] * n for _ in range(m)]\n        \n        for i in range(m):\n            \n            for j in range(n):\n                \n                if grid[i][j] != 0:\n                    \n                    if j > 0 and grid[i][j-1] !=0:\n                        left[i][j] = grid[i][j] + left[i][j-1]\n                    else:\n                        left[i][j] = grid[i][j]\n                        \n                    if i > 0 and grid[i-1][j]!=0:\n                        height[i][j] = grid[i][j] + height[i-1][j]\n                    else:\n                        height[i][j] = grid[i][j]\n                      \n                        \n        for i in range(m):\n            \n            for j in range(n):\n                \n                if grid[i][j] !=0:\n                    \n                    for k in range(min(left[i][j],height[i][j])):\n                        \n                        if i-k>= 0 and left[i-k][j] >= k+1 and j-k >=0 and height[i][j-k] >= k+1:\n                            res = max(res,(k+1)**2)\n                            \n        return res\n```
[]