#1139

Largest 1-Bordered Square

specialist · 850 · lc medium +31 · verified · 52% accepted · 768 likes · top 42%

Description

Given a 2D grid of 0s and 1s, find the largest square subgrid whose entire border consists of 1s. Return the number of cells in that square, or 0 if no such square exists.

Example 1:

Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
Output: 9

Example 2:

Input: grid = [[1,1,0,0]]
Output: 1

Code

1
2
3