Largest 1-Bordered Square

Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid does not exist in the grid.

Example 1
1 1 1
1 0 1
1 1 1
Inputgrid = [[1,1,1],[1,0,1],[1,1,1]]
Output9
The entire 3 by 3 grid has all 1s on its border, so its area is 9.
Example 2
1 1 0 0
Inputgrid = [[1,1,0,0]]
Output1
The largest square subgrid with a border of 1s is any single cell containing 1, so its area is 1.

Constraints

  • 1 <= grid.length <= 100
  • 1 <= grid[0].length <= 100
  • grid[i][j] is 0 or 1

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate