class Solution(object):
    def closedIsland(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        if not grid:
            return 0
        m,n=len(grid),len(grid[0])     
        
        def dfs(i,j,val):
            if(0<=i<m and 0<=j<n and grid[i][j]==0):
                grid[i][j]=val
                dfs(i+1,j,-1)
                dfs(i-1,j,-1)
                dfs(i,j+1,-1)
                dfs(i,j-1,-1)
        
        for i in range(m):
            for j in range(n):
                if(i==0 or i==m-1 or j==0 or j==n-1) and grid[i][j]==0:
                    dfs(i,j,-1)
        
        res=0
        for i in range(m):
            for j in range(n):
                if grid[i][j]==0:
                    dfs(i,j,-1)
                    res+=1
        
        return res

