<p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>grid</code>. Your initial position is at the <strong>top-left</strong> cell <code>(0, 0)</code>.</p>

<p>Starting from the cell <code>(i, j)</code>, you can move to one of the following cells:</p>

<ul>
	<li>Cells <code>(i, k)</code> with <code>j &lt; k &lt;= grid[i][j] + j</code> (rightward movement), or</li>
	<li>Cells <code>(k, j)</code> with <code>i &lt; k &lt;= grid[i][j] + i</code> (downward movement).</li>
</ul>

<p>Return <em>the minimum number of cells you need to visit to reach the <strong>bottom-right</strong> cell</em> <code>(m - 1, n - 1)</code>. If there is no valid path, return <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex1.png" style="width: 271px; height: 171px;" />
<pre>
<strong>Input:</strong> grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The image above shows one of the paths that visits exactly 4 cells.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex2.png" style="width: 271px; height: 171px;" />
<pre>
<strong>Input:</strong> grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>The image above shows one of the paths that visits exactly 3 cells.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/26/ex3.png" style="width: 181px; height: 81px;" />
<pre>
<strong>Input:</strong> grid = [[2,1,0],[1,0,0]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> It can be proven that no path exists.
</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;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= grid[i][j] &lt; m * n</code></li>
	<li><code>grid[m - 1][n - 1] == 0</code></li>
</ul>

class Solution:
    def minimumVisitedCells(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        if m == 1 and n == 1:
            return 1
        
        # Initialize the queue with the starting point
        queue = deque([(0, 0)])
        level = 1
        
        # Initialize Union-Find structures
        usv = [[i for i in range(m + 1)] for _ in range(n)]
        ush = [[i for i in range(n + 1)] for _ in range(m)]
        
        while queue:
            level += 1
            for _ in range(len(queue)):
                r, c = queue.popleft()
                
                # Check downward moves
                nr = self.getRoot(r + 1, usv, c)
                while nr < m and nr <= r + grid[r][c]:
                    if nr == m - 1 and c == n - 1:
                        return level
                    queue.append((nr, c))
                    nr = self.doUnion(nr, nr + 1, usv, c)
                
                # Check rightward moves
                nc = self.getRoot(c + 1, ush, r)
                while nc < n and nc <= c + grid[r][c]:
                    if r == m - 1 and nc == n - 1:
                        return level
                    queue.append((r, nc))
                    nc = self.doUnion(nc, nc + 1, ush, r)
        
        # If no valid path is found
        return -1

    def getRoot(self, node: int, usAry: List[List[int]], i: int) -> int:
        while node != usAry[i][node]:
            usAry[i][node] = usAry[i][usAry[i][node]]
            node = usAry[i][node]
        return node

    def doUnion(self, node1: int, node2: int, usAry: List[List[int]], i: int) -> int:
        r1 = self.getRoot(node1, usAry, i)
        r2 = self.getRoot(node2, usAry, i)
        usAry[i][r1] = r2
        return r2

54100