Practice/Google/Leetcode 1254. Number of Closed Islands
CodingMust
You are given a 2D binary grid where each cell contains either a 0 or a 1. The value 0 represents water and the value 1 represents land.
An isolated island is a connected group of 0s (water cells) that is completely surrounded by 1s (land cells). Crucially, the group must not touch any edge of the grid. Two water cells are considered connected if they are adjacent horizontally or vertically (not diagonally).
Your task is to count how many isolated islands exist in the grid.
Example 1:
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: 2 Explanation: There are two distinct groups of 0s that are completely surrounded by 1s. The 0s in the rightmost column touch the boundary, so they don't count.
Example 2:
Input: grid = [[0,0,1,0,0], [0,1,0,1,0], [0,1,1,1,0]] Output: 0 Explanation: All water cells touch the boundary of the grid, so there are no isolated islands.
Example 3:
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: The outer ring of 0s forms one isolated island, and the single 0 in the center forms another.