<p>Given a 2D&nbsp;<code>grid</code> consists of <code>0s</code> (land)&nbsp;and <code>1s</code> (water).&nbsp; An <em>island</em> is a maximal 4-directionally connected group of <code><font face="monospace">0</font>s</code> and a <em>closed island</em>&nbsp;is an island <strong>totally</strong>&nbsp;(all left, top, right, bottom) surrounded by <code>1s.</code></p>

<p>Return the number of <em>closed islands</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2019/10/31/sample_3_1610.png" style="width: 240px; height: 120px;" /></p>

<pre>
<strong>Input:</strong> 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]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
Islands in gray are closed because they are completely surrounded by water (group of 1s).</pre>

<p><strong class="example">Example 2:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2019/10/31/sample_4_1610.png" style="width: 160px; height: 80px;" /></p>

<pre>
<strong>Input:</strong> grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1,1,1],
&nbsp;              [1,0,0,0,0,0,1],
&nbsp;              [1,0,1,1,1,0,1],
&nbsp;              [1,0,1,0,1,0,1],
&nbsp;              [1,0,1,1,1,0,1],
&nbsp;              [1,0,0,0,0,0,1],
               [1,1,1,1,1,1,1]]
<strong>Output:</strong> 2
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= grid.length, grid[0].length &lt;= 100</code></li>
	<li><code>0 &lt;= grid[i][j] &lt;=1</code></li>
</ul>

class Solution:
    def closedIsland(self, grid: List[List[int]]) -> int:
        pass























class Solution:
    def closedIsland(self, grid: List[List[int]]) -> int:
        # check if area of 0s are connected to the boundary.
        # if connected, not island. 
        # if not connected, island. 
        # record coords of islands into seen = set()

        def bfs(y, x, seen_all):
            queue = deque([(y, x)])
            seen = set([(y, x)])
            isClosed = True
            while queue:
                y, x = queue.popleft()
                if y == 0 or y == m-1 or x == 0 or x == n-1:
                    isClosed = False
                
                for dydx in ((0, 1), (0, -1), (1, 0), (-1, 0)):
                    yy, xx = y + dydx[0], x + dydx[1]
                    if not (0<=yy<m and 0<=xx<n):
                        continue
                    if grid[yy][xx] == 0:
                        if (yy, xx) not in seen:
                            if (yy, xx) not in seen_all:
                                seen.add((yy, xx))
                                queue.append((yy, xx))
            return isClosed, seen

        m, n = len(grid), len(grid[0])
        seen_sea = set()
        seen_island = set()
        cnt = 0
        for y in range(m):
            for x in range(n):
                if (y, x) not in seen_sea and (y, x) not in seen_island:
                    if grid[y][x] == 0:
                        isClosed, seen = bfs(y, x, seen_sea)
                        if isClosed:
                            seen_island.update(seen)
                            cnt += 1
                        else:
                            seen_sea.update(seen)
        return cnt





















class Solution:
    def closedIsland(self, grid: List[List[int]]) -> int:
        seen_closed = set()
        seen_open = set()

        def bfs(y, x):
            # return True if it does not touch outer boundary (closed)
            # return False if it touch outer boundary (open)
            queue = deque([(y, x)])
            seen = set([(y, x)])
            isClosed = True
            while queue:
                y, x = queue.popleft()
                if y == 0 or y == m-1 or x == 0 or x == n-1:
                    isClosed = False

                for dydx in ((0,1), (0,-1), (1,0), (-1,0)):
                    yy, xx = y + dydx[0], x + dydx[1]
                    if not (0<=yy<m and 0<=xx<n):
                        continue
                    if grid[yy][xx] == 0:
                        if (yy, xx) not in seen:
                            seen.add((yy, xx))
                            queue.append((yy, xx))
            return isClosed, seen

        m, n = len(grid), len(grid[0])
        cnt = 0
        for y in range(1, m-1):
            for x in range(1, n-1):
                if grid[y][x] == 0:
                    if (y, x) not in seen_closed and (y, x) not in seen_open:
                        isClosed, seen = bfs(y, x)
                        if isClosed:
                            cnt += 1
                            seen_closed = seen_closed.union(seen)
                        else:
                            seen_open = seen_open.union(seen)
        return cnt


























class Solution:
    def closedIsland(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])

        def bfs(y, x):
            queue = deque([(y, x)])
            seen.add((y, x))
            isClosed = True
            while queue:
                y, x = queue.popleft()
                if y == m-1 or y == 0 or x == 0 or x == n-1:
                    isClosed = False
                for dydx in ((0, 1), (0, -1), (1,0), (-1,0)):
                    yy, xx = y + dydx[0], x + dydx[1]
                    if not (0<=yy<m and 0<=xx<n):
                        continue
                    if (yy, xx) not in seen and grid[yy][xx] == 0:
                        queue.append((yy, xx))
                        seen.add((yy, xx))
            return isClosed

        seen = set()
        cnt = 0
        for y in range(1, m-1):
            for x in range(1, n-1):
                if grid[y][x] == 0:
                    if (y, x) not in seen:
                        seen.add((y, x))
                        if bfs(y, x):
                            cnt += 1
        return cnt



























class Solution:
    def closedIsland(self, grid: List[List[int]]) -> int:
        seen = set()
        m, n = len(grid), len(grid[0])
        cnt = 0

        def bfs(y, x):
            seen.add((y, x))
            queue = deque([(y, x)])
            isClosed = True
            while queue:
                y, x = queue.popleft()
                if y == m-1 or y == 0 or x == 0 or x == n-1:
                    isClosed = False

                for dydx in ((0,1), (0,-1), (1,0), (-1,0)):
                    yy, xx = y + dydx[0], x + dydx[1]
                    if not (0<=yy<m and 0<=xx<n):
                        continue
                    if grid[yy][xx] == 0:
                        if (yy, xx) not in seen:
                            seen.add((yy, xx))
                            queue.append((yy, xx))
            return isClosed
        
        for y in range(0, m):
            for x in range(0, n):
                if grid[y][x] == 0:
                    if (y, x) not in seen:
                        if bfs(y, x):
                            cnt += 1
        return cnt





















class Solution:
    def closedIsland(self, grid: List[List[int]]) -> int:
        seen = set()
        m, n = len(grid), len(grid[0])

        def search_dfs(y, x):
            # return True if surrounded
            seen.add((y, x))
            isIsland = True

            if y == m-1 or y == 0 or x == n-1 or x == 0:
                isIsland = False
            
            for dydx in ((0,1), (1,0), (0,-1), (-1,0)):
                yy, xx = y + dydx[0], x + dydx[1]
                if not (0<=yy<m and 0<=xx<n):
                    continue
                if grid[yy][xx] == 0 and (yy, xx) not in seen:
                    seen.add((yy, xx))
                    if not search_dfs(yy, xx):
                        isIsland = False
            return isIsland
        
        cnt = 0
        for y in range(m):
            for x in range(n):
                if grid[y][x] == 0 and (y, x) not in seen:
                    if search_dfs(y, x):
                        cnt += 1
        return cnt

















class Solution:
    def closedIsland(self, grid: List[List[int]]) -> int:
        '''
        0: land
        1: water
        Find island closed

        Explore all 0 cells
        - Perform BFS
        - Record each visited cells, flip 0 --> -1
        - if found edges, record isClosed = False
        - return isClosed
        '''

        def bfs(y, x, seen):
            # x, y: seed location with grid[y][x] = 0
            # return True if closed island
            if x == 0 or x == n-1 or y == 0 or y == m-1:
                return False
            queue = deque([(y, x)])
            isClosed = True
            while queue:
                y, x = queue.popleft()
                for dydx in ((0,1), (0,-1), (1,0), (-1,0)):
                    yy, xx = y + dydx[0], x + dydx[1]
                    if not (0<=yy<m and 0<=xx<n):
                        isClosed = False
                        continue
                    if grid[yy][xx] == 0:
                        if not (1<=yy<m-1 and 1<=xx<n-1):
                            isClosed = False
                            continue
                        if (yy, xx) not in seen:
                            seen.add((yy, xx))
                            queue.append((yy, xx))
            return isClosed

        cnt = 0
        seen = set()
        m, n = len(grid), len(grid[0])
        for y in range(1, m-1):
            for x in range(1, n-1):
                if grid[y][x] == 0 and (y, x) not in seen:
                    if bfs(y, x, seen):
                        cnt += 1
        return cnt





















class Solution:
    def closedIsland(self, grid: List[List[int]]) -> int:
        # 0: land
        # 1: water
        # number of island
        # 1. find a seed y, x
        # 2. perform bfs from y, x
        # 3. if reaches bounary --> not island
        # 4. if not --> island
        # change visited land to -1

        m, n = len(grid), len(grid[0])

        def bfs(y, x):
            # return True if island
            queue = deque([(y, x)])
            isIsland = True
            while queue:
                y, x = queue.popleft()
                grid[y][x] = -1
                for dydx in ((0, 1), (0,-1), (1,0), (-1,0)):
                    yy, xx = y + dydx[0], x + dydx[1]
                    if not (0<=yy<m and 0<=xx<n):
                        isIsland = False
                        continue
                    if grid[yy][xx] == 0:
                        queue.append((yy, xx))
            return isIsland

        cnt = 0
        for y in range(1, m-1):
            for x in range(1, n-1):
                if grid[y][x] == 0:
                    if bfs(y, x):
                        cnt += 1
        return cnt
17900