#1020
Number of Enclaves
pupil · 535 · lc medium +28 · verified · 71.5% accepted · 4,598 likes · top 80%
Description
You are given an m x n binary matrix grid (0 = sea, 1 = land). Moving means stepping 4-directionally to an adjacent land cell or walking off the grid boundary.
Return the number of land cells from which it is impossible to reach the grid boundary.
Example 1:
Input: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output: 3
Explanation: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.
Example 2:
Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output: 0
Explanation: All 1s are either on the boundary or can reach the boundary.
Code
1
2
3