<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:
        dirs = [[-1, 0], [0, -1], [1, 0], [0, 1]]
        m, n = len(grid), len(grid[0])

        parents = [[(r, c) for c in range(n)] for r in range(m)]

        def find(r, c):
            if parents[r][c] != (r, c):
                parents[r][c] = find(parents[r][c][0], parents[r][c][1])
            return parents[r][c]

        for r in range(m):
            for c in range(n):
                if grid[r][c] == '0':
                    continue
                ps = []
                p = find(r, c)
                for d in dirs:
                    nr, nc = r + d[0], c + d[1]
                    if nr < 0 or nc < 0 or nr >= m or nc >= n:
                        continue
                    if grid[nr][nc] == '0':
                        continue
                    
                    #print(r, c, nr, nc)
                    ps.append(find(nr, nc))
                if len(ps) == 0:
                    continue
                gp = ps[0]
                parents[p[0]][p[1]] = gp
                for i in range(1, len(ps)):
                    tup = ps[i]
                    parents[tup[0]][tup[1]] = gp

        for r in range(m):
            print(grid[r])

        print()

        for r in range(m):
            print(parents[r])

        res = 0
        for r in range(m):
            for c in range(n):
                if parents[r][c] == (r, c) and grid[r][c] == '1':
                    res += 1
        return res
25183