# Leetcode 1254. Number of closed islands ## DFS ```python= class Solution: def closedIsland(self, grid: List[List[int]]) -> int: output = 0 directional_map = [[-1,0],[0,-1],[1,0],[0,1]] rows = len(grid) cols = len(grid[0]) def dfs(node: int): x = node // cols y = node % cols if grid[x][y] == 1 or grid[x][y] == -1: return True grid[x][y] = -1 res = True for _x,_y in directional_map: new_x = x + _x new_y = y + _y if new_x < 0 or new_y < 0 or new_x >= rows or new_y >= cols: res = False continue if not dfs(new_x * cols + new_y): res = False return res for node in range(rows * cols): x = node // cols y = node % cols if grid[x][y] == 0 and dfs(node): output += 1 return output ```