<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 UnionFind:
    def __init__(self, grid):
        ROWS, COLS = len(grid), len(grid[0])
        self.par = [[(r, c) for c in range(COLS)] for r in range(ROWS)]  # Fix here for correct tuple generation
        self.rank = [[1 for _ in range(COLS)] for _ in range(ROWS)]

    def find(self, node):
        r, c = node
        if self.par[r][c] != node:
            self.par[r][c] = self.find(self.par[r][c])  # Path compression
        return self.par[r][c]

    def union(self, n1, n2):
        root1 = self.find(n1)
        root2 = self.find(n2)
        
        if root1 == root2:
            return 0  # No new union

        r1, c1 = root1
        r2, c2 = root2
        
        # Union by rank
        if self.rank[r1][c1] >= self.rank[r2][c2]:
            self.par[r2][c2] = root1
            self.rank[r1][c1] += self.rank[r2][c2]
        else:
            self.par[r1][c1] = root2
            self.rank[r2][c2] += self.rank[r1][c1]

        return 1  # New union happened

class Solution:
    def numIslands(self, grid: list[list[str]]) -> int:
        if not grid or not grid[0]:
            return 0
        
        uf = UnionFind(grid)
        ROWS, COLS = len(grid), len(grid[0])
        directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
        numIslands = 0

        for r in range(ROWS):
            for c in range(COLS):
                if grid[r][c] == "1":
                    numIslands += 1  # Count the land as a new island first
                    for dr, dc in directions:
                        nr, nc = r + dr, c + dc
                        if 0 <= nr < ROWS and 0 <= nc < COLS and grid[nr][nc] == "1":
                            numIslands -= uf.union((r, c), (nr, nc))

        return numIslands

24918