1139. Largest 1-Bordered Square
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int largest1BorderedSquare(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        int[][] left = new int[m][n], up = new int[m][n];
        int maxSize = 0;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    left[i][j] = (j > 0) ? left[i][j - 1] + 1 : 1;
                    up[i][j] = (i > 0) ? up[i - 1][j] + 1 : 1;
                    int side = Math.min(left[i][j], up[i][j]);
                    while (side > maxSize) {
                        if (up[i][j - side + 1] >= side && left[i - side + 1][j] >= side) {
                            maxSize = side;
                        }
                        side--;
                    }
                }
            }
        }
        return maxSize * maxSize;
    }
}

```
['Java']