1000. Number of Closed Islands

EasyGraphsGridDFSGraph

Given a grid where 0 is land and 1 is water, return the number of closed islands — groups of land (connected 4-directionally) that are completely surrounded by water and do not touch the grid border. The input is JSON {grid}.

Input: JSON {grid}.

Output: Integer — the number of closed islands.

Examples

Example 1
Input: {"grid":[[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]}
Output: 2
Explanation: Two enclosed land regions.
Example 2
Input: {"grid":[[0]]}
Output: 0
Explanation: Border land is not closed.
Example 3
Input: {"grid":[[1,1,1],[1,0,1],[1,1,1]]}
Output: 1
Explanation: One enclosed cell.

Constraints

Asked by

OracleGoogleMicrosoftMetaAmazon
Solve this problem in the editor →