""" https://leetcode.com/problems/number-of-closed-islands/
opposite 1020
"""
from header import *

class Solution:
    def closedIsland(self, A: List[List[int]]) -> int:
        D = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        
        def dfs(x, y):
            if not (0<=x<len(A) and 0<=y<len(A[0])): return False
            elif A[x][y]==1: return True
            A[x][y] = 1
            ans = True
            for dx, dy in D:
                ans &= dfs(x+dx, y+dy)
            return ans
        
        ans = 0
        for i in range(len(A)):
            for j in range(len(A[0])):
                if not(not i or not j or i==len(A)-1 or j==len(A[0])-1) and A[i][j]==0:
                    if dfs(i, j): ans += 1
        return ans
3d4f321b23172cc946bab183f319a758f34123d4
<mask token>

<mask token>


class Solution:
    <mask token>

<mask token>


class Solution:

    def closedIsland(self, A: List[List[int]]) ->int:
        D = [(0, 1), (0, -1), (1, 0), (-1, 0)]

        def dfs(x, y):
            if not (0 <= x < len(A) and 0 <= y < len(A[0])):
                return False
            elif A[x][y] == 1:
                return True
            A[x][y] = 1
            ans = True
            for dx, dy in D:
                ans &= dfs(x + dx, y + dy)
            return ans
        ans = 0
        for i in range(len(A)):
            for j in range(len(A[0])):
                if not (not i or not j or i == len(A) - 1 or j == len(A[0]) - 1
                    ) and A[i][j] == 0:
                    if dfs(i, j):
                        ans += 1
        return ans

<mask token>
from header import *


class Solution:

    def closedIsland(self, A: List[List[int]]) ->int:
        D = [(0, 1), (0, -1), (1, 0), (-1, 0)]

        def dfs(x, y):
            if not (0 <= x < len(A) and 0 <= y < len(A[0])):
                return False
            elif A[x][y] == 1:
                return True
            A[x][y] = 1
            ans = True
            for dx, dy in D:
                ans &= dfs(x + dx, y + dy)
            return ans
        ans = 0
        for i in range(len(A)):
            for j in range(len(A[0])):
                if not (not i or not j or i == len(A) - 1 or j == len(A[0]) - 1
                    ) and A[i][j] == 0:
                    if dfs(i, j):
                        ans += 1
        return ans

""" https://leetcode.com/problems/number-of-closed-islands/
opposite 1020
"""
from header import *

class Solution:
    def closedIsland(self, A: List[List[int]]) -> int:
        D = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        
        def dfs(x, y):
            if not (0<=x<len(A) and 0<=y<len(A[0])): return False
            elif A[x][y]==1: return True
            A[x][y] = 1
            ans = True
            for dx, dy in D:
                ans &= dfs(x+dx, y+dy)
            return ans
        
        ans = 0
        for i in range(len(A)):
            for j in range(len(A[0])):
                if not(not i or not j or i==len(A)-1 or j==len(A[0])-1) and A[i][j]==0:
                    if dfs(i, j): ans += 1
        return ans