Count Closed Islands

DEPTH-FIRST SEARCH
BREADTH-FIRST SEARCH
MATRIX

Problem

Given a 2D grid consisting of 1s (land) and 0s (water), an island is considered closed if it is completely surrounded by water and is formed by connecting adjacent lands horizontally or vertically.

You may assume all four edges of the grid are surrounded by water. Determine the number of closed islands in the grid.

Examples

closedIslands([ [1, 0, 1], [0, 1, 0], [1, 0, 1] ]) // returns 1 /* There is one closed island at the center of the grid, surrounded by '0's from all sides. */
Loading...
1
0
1
0
1
0
1
0
1