<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:
        res = 0
        if not grid:
            return res
        m = len(grid)
        n = len(grid[0])
        seen = set()

        def dfs(r, c):
            if r not in range(m) or c not in range(n) or (r, c) in seen or grid[r][c] == "0":
                return
            seen.add((r, c))
            directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
            for dr, dc in directions:
                nr, nc = dr+r, dc+c
                dfs(nr, nc)
        
        for i in range(m):
            for j in range(n):
                if (i, j) not in seen and grid[i][j] == "1":
                    res += 1
                    dfs(i, j)
        return res

                 





















        # # bfs
        # if not grid:
        #     return 0
        # m = len(grid)
        # n = len(grid[0])
        # res = 0

        # def bfs(r, c):
        #     q = deque()
        #     q.append((r, c))
        #     grid[r][c] = "0"
        #     while q:
        #         sr, sc = q.popleft()
        #         directions = [(0, 1), (1, 0), (-1, 0), (0, -1)]
        #         for dr, dc in directions:
        #             nr, nc = dr+sr, dc+sc
        #             if (nr in range(m) and nc in range(n) and grid[nr][nc] == "1"):
        #                 grid[nr][nc] = "0"
        #                 q.append((nr, nc))

        # for i in range(m):
        #     for j in range(n):
        #         if grid[i][j] == "1" :
        #             res += 1
        #             bfs(i, j)
        # return res 

        # # # dfs的solution
        # # if not grid:
        # #     return 0
        # # islands = 0
        # # visit = set()
        # # rows, cols = len(grid), len(grid[0])

        # # def dfs(r, c):
        # #     if (r not in range(rows)
        # #     or c not in range(cols)
        # #     or grid[r][c] == "0"
        # #     or (r, c) in visit):
        # #         return
        # #     visit.add((r, c))
        # #     directions = [(0, 1), (1, 0), (-1, 0), (0, -1)]
        # #     for dr, dc in directions:
        # #         dfs(r +  dr, c + dc)

        # # for r in range(rows):
        # #     for c in range(cols):
        # #         if grid[r][c] == "1" and (r, c) not in visit:
        # #             islands += 1
        # #             dfs(r, c)
        # # return islands 
        # # # 时间复杂度O(m*n),空间复杂度O(m*n)
27171