Brute Force: O(N*M*min(N, M)^2), O(1)
Intuition\nJust write a brute force algorithm. Treat the start row and start col as the top left edge of the square, then search for 1ns on its sides until we h
# Intuition\nJust write a brute force algorithm. Treat the start row and start col as the **top left edge** of the square, then search for 1ns on its sides until we have an invalid square. Kinda suprised this passed as this can be O(N^4)\n\n\n\n# Code\n### Time Complexity: $$O(N * M * min(N, M)^2)$$\n### Space Complexity: $$O(1)$$\n```\nclass Solution:\n    def largest1BorderedSquare(self, grid: List[List[int]]) -> int:\n        N,M = len(grid), len(grid[0])\n        largest_square = 0\n        for start_r in range(N):\n            for start_c in range(M):\n                for i in range(0, min(M-start_c, N-start_r)):\n                    valid_square = True\n                    if not (grid[start_r][start_c+i] and grid[start_r+i][start_c]):\n                        valid_square = False\n                        break\n                    for j in range(0, i+1):\n                        if not (grid[start_r+i][start_c+i-j] and grid[start_r+i-j][start_c+i]):\n                            valid_square = False\n                            break\n                    if valid_square:\n                        largest_square = max(largest_square, (i+1)*(i+1))\n        return largest_square\n\n\n\n```
['Python3']