Number of Closed Islands
Given a 2D grid consisting of 0s representing land and 1s representing water, an island is a maximal 4-directionally connected group of 0s.
A closed island is an island totally surrounded by 1s on all four sides: left, top, right, and bottom.
Return the number of closed islands.
Example 1
1 1 1 1 1 1 1 0 1 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 1 0 0 0 0 1 0 1 1 1 1 1 1 1 1 0
Input
grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]Output
2Islands in gray are closed because they are completely surrounded by water, so there are 2 closed islands.
Example 2
0 0 1 0 0 0 1 0 1 0 0 1 1 1 0
Input
grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]Output
1There is exactly 1 island of land cells that is completely surrounded by water.
Constraints
- 1 <= grid.length, grid[0].length <= 100
- 0 <= grid[i][j] <=1