<p>You are given an <code>m x n</code> binary matrix <code>grid</code>, where <code>0</code> represents a sea cell and <code>1</code> represents a land cell.</p>

<p>A <strong>move</strong> consists of walking from one land cell to another adjacent (<strong>4-directionally</strong>) land cell or walking off the boundary of the <code>grid</code>.</p>

<p>Return <em>the number of land cells in</em> <code>grid</code> <em>for which we cannot walk off the boundary of the grid in any number of <strong>moves</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/enclaves1.jpg" style="width: 333px; height: 333px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/enclaves2.jpg" style="width: 333px; height: 333px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All 1s are either on the boundary or can reach the boundary.
</pre>

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

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 500</code></li>
	<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

# directions = left, right, up, down 
# 0 = sea cell
# 1 = land cell
# a move is walking from one land cell to another adjacent land cell OR walking off the boundary of the grid 

# goal: return the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves 
# use DFS

class Solution:
    def numEnclaves(self, grid: List[List[int]]) -> int:
        # create two variables cols and rows 
        rows = len(grid)
        cols = len(grid[0])

        # create 2D array called visited to keep track of visited cells
        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)]

        # dfs function to perform the traversal - for each cell pass x,y, and grid as the parameters (x = row, y=col)
        def dfs(row, col):
            # if the cell (row, col) is out of bounds of grid or is a water cell or is an already visited cell - don't do anything and return
            if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0 or visited[row][col]:
                return
            # otherwise - visit the cell and mark it as visited
            visited[row][col] = True
            # call dfs recursively from each of the neighbours of (row,col)
            for dir_row, dir_col in directions:
                dfs(row + dir_row, col + dir_col)

        # iterate over all the cells at the grid's boundary and for every such cell (row, col) check if it's a land cell or not
        for row in range(rows):
            for col in range(cols):
                # if it is a land cell and it has not been visited yet, begin a DFS traversal from (row, col) cell
                if (row == 0 or row == rows - 1 or col == 0 or col == cols - 1) and grid[row][col] == 1 and not visited[row][col]:
                    dfs(row, col)
        
        # create an answer variable ans to count required number of land cells
        ans = 0 
        # iterate over all the cells of grid and count number of unvisited land cells - for each unvisited land cell increment ans by 1
        for row in range(rows):
            for col in range(cols):
                if grid[row][col] == 1 and not visited[row][col]:
                    ans += 1
        # return ans
        return ans

'''
time complexity:
- in the worst case DFS visits every cell once which is m * n 
- therefore T: O(m * n)

space complexity:
- the space complexity of the visited array is O(m * n)
- max depth of the DFS call stack can be as large as the number of land cells - in worst case is O(m * n)
- therefore S: O(m * n)
'''
33145