Simple Python O(N**3)
\nclass Solution:\n    def largest1BorderedSquare(self, grid: List[List[int]]) -> int:\n        memo_h = {}\n        memo_v = {}\n        for i in range(len(gri
```\nclass Solution:\n    def largest1BorderedSquare(self, grid: List[List[int]]) -> int:\n        memo_h = {}\n        memo_v = {}\n        for i in range(len(grid)):\n            for j in range(len(grid[0])):\n                memo_v[(i,j)] = 0\n                for k in range(i,len(grid)):\n                    if grid[k][j]:\n                        memo_v[(i,j)] = k-i+1\n                    else:\n                        break\n                memo_h[(i,j)] = 0\n                for k in range(j,len(grid[0])):\n                    if grid[i][k]:\n                        memo_h[(i,j)] = k - j+1\n                    else:\n                        break\n        res = 0\n        for i in range(len(grid)):\n            for j in range(len(grid[0])):\n                k = 1\n                while i+k <= len(grid) and j + k <= len(grid[0]):\n                    if memo_v[(i,j)] >= k and memo_h[(i,j)] >= k and memo_v[(i,j+k-1)] >= k and memo_h[(i+k-1,j)] >= k:\n                        res = max(res,k*k)\n                    k += 1\n        return res\n```
[]