Python, dp solution with explanation
python\n\'\'\'\ni+1 -> next row, j+1 -> next column\ndp[i][j][0] means that there are number of continous 1 which forms a horizontal edge end at position (i, j)
```python\n\'\'\'\ni+1 -> next row, j+1 -> next column\ndp[i][j][0] means that there are number of continous 1 which forms a horizontal edge end at position (i, j).\ndp[i][j][1] means that there are number of continous 1 which forms a vertical edge end at position (i, j).\nif the number at the position (i, j) is 1, calculate length of the vertical and horizontal edges first, and we start to enumerate squares formed with both edges.\nFind the minimum length of two edge, and enumerate the edge length from 0(ans) to minimum length:\n\tcheck length of the vertical edge at position (i, j-minimum length+1) and the horizontal edge at position (i-minimum length+1, j) can form a square\n\ntc is O(n^3), sc is O(n^2)\n\'\'\'\nclass Solution:\n    def largest1BorderedSquare(self, grid: List[List[int]]) -> int:\n        rowNum, colNum = len(grid), len(grid[0])\n        dp = [[[0] * 2 for _ in range(colNum)] for _ in range(rowNum)]\n        ans = 0\n        for r, row in enumerate(grid):\n            for c, n in enumerate(row):\n                if n == 1:\n                    dp[r][c][0] = dp[r][c-1][0] + 1 if c - 1 >= 0 else 1\n                    dp[r][c][1] = dp[r-1][c][1] + 1 if r - 1 >= 0 else 1\n                    lenOfEdge = dp[r][c][0] if dp[r][c][0] < dp[r][c][1] else dp[r][c][1]\n                    for e in range(ans+1, lenOfEdge+1):\n                        if dp[r][c-e+1][1] >= e and dp[r-e+1][c][0] >= e and e > ans:\n                            ans = e\n        return ans * ans\n```
['Dynamic Programming', 'Python']