<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>

# 0 = land and 1 = water
# island = maximal 4 directionally connected group of 0s
# closed island = island totally surrounded by 1s (all left, top, right, bottom)
# goal return number of closed islands

# use dfs

class Solution:
    def closedIsland(self, grid: List[List[int]]) -> int:
        # initialize rows and cols
        rows = len(grid)
        cols = len(grid[0])
        # initialize a 2D array visited to keep track of cells you visit
        visited = [[False for _ in range(cols)] for _ in range(rows)]
        # set directions (left, right, up, down)
        directions = [(-1,0), (1,0), (0,-1), (0,1)]
        # answer variable to keep track of closed islands - initialize with 0
        ans = 0

        def dfs(row, col):
            # if cell is out of bounds - means there was a land cell at the boundary of the grid whose neighbour is (row,col)
            if  row < 0 or row >= rows or col < 0 or col >= cols:
                # return false to indicate this island is not closed
                return False
            # else if a water cell or an already visited cell - return true
            elif grid[row][col] == 1 or visited[row][col]:
                return True
            # else visit the cell and mark as visited
            else:
                visited[row][col] = True
                # create a bool variable isClosed that stores whether or not the current island is closed or not
                isClosed = True # initialized to true since no nodes found that are on grid boundary yet

            # recursively visit neighbouring cells
            for dir_row, dir_col in directions:
                if not dfs(row + dir_row, col + dir_col):
                # if any of the directions leads to a cell in the island at boundary, the island is not closed - mark isClosed false
                    isClosed = False
            
            return isClosed
        
        # iterate over all the cells of grid and for every cell (row,col) check if its a land cell or not
        for row in range(1, rows - 1):
            for col in range(1, cols-1):
                #if its a land cell and has not been visited yet begin dfs traversal
                if grid[row][col] == 0 and not visited[row][col]:
                    # if dfs returns true
                    if dfs(row,col):
                        # increment answer by 1
                        ans += 1
        # return answer
        return ans

# time complexity: O(m * n)
# space complexity: O(m * n)
                
17200