<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p>

<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [
  [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;]
]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;]
]
<strong>Output:</strong> 3
</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;= 300</code></li>
	<li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
</ul>

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:

        n_rows, n_cols = len(grid), len(grid[0])
        visited = set()
        count = 0

        # """Iterative"""
        # def check_nearby(r,c):
        #     """should add all nearby 1s to set visited, iteratively"""
        #     visited.add((r,c))
        #     queue = deque()
        #     queue.append((r,c))

        #     while queue:
        #         """if we make popleft to pop only, then it will become dfs"""
        #         this_r, this_c = queue.popleft()
        #         # print(f'got r={this_r}, c={this_c}')

        #         dirs = [(-1,0), (1,0), (0,1), (0,-1)]
        #         for dr, dc in dirs:
        #             check_r, check_c = this_r + dr, this_c + dc
        #             # print(f'\t checking {check_r, check_c} ..., visited = {visited}')
                    
        #             if 0 <= check_r < n_rows
        #             and 0 <= check_c < n_cols 
        #             and grid[check_r][check_c] == "1" 
        #             and (check_r, check_c) not in visited:
                        
        #                 queue.append((check_r, check_c))
        #                 visited.add((check_r, check_c))

        #                 # print(f'found nice at {(check_r, check_c)}, visited = {visited}')

        #     return

        def dfs(r,c):
            if (not (0<=r<n_rows)) or (not(0<=c<n_cols)) or ((r,c) in visited) or (grid[r][c] != '1'):
                return 
            
            # have now reached a cell in bounds with value 1
            visited.add((r,c))
            dfs(r+1,c)
            dfs(r-1,c)
            dfs(r,c+1)
            dfs(r,c-1)


        for r in range(n_rows):
            for c in range(n_cols):
                if (r,c) not in visited and grid[r][c] == "1":
                    # check_nearby(r, c)
                    dfs(r,c)
                    count += 1

        # print(f'count = {count} visited = {visited}')

        return count

        
27701