Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally connected group of 0s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s. Return the number of closed islands.
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: Two land groups are fully ringed by water, so both count. Land touching the right edge leaks off the border and is not closed.
Example 2
Input: grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
Output: 1
Explanation: Only the single land cell in the middle is walled in by water on all four sides. Every other land cell sits on an edge, so it is open.
Example 3
Input: grid = [[1,1,1,1,1,1,1],[1,0,0,0,0,0,1],[1,0,1,1,1,0,1],[1,0,1,0,1,0,1],[1,0,1,1,1,0,1],[1,0,0,0,0,0,1],[1,1,1,1,1,1,1]]
Output: 2
Explanation: The outer ring of land is surrounded by water, and the lone cell in the very center is its own closed island, giving two.
An island is closed only if it never touches the grid border. Flood-fill each island and let the recursion report back whether every branch stayed inside the grid.
Run checks all cases above. Submit evaluates all test cases.