<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 Node:
    def __init__(self, step, k):
        self.step = step
        self.k = k

    def __lt__(self, other):
        if self.step == other.step:
            return self.k < other.k
        return self.step < other.step

    def __str__(self):
        return f"[{self.step} - {self.k}]"

class Solution:
    def getActualElement(self, rowElement, colElement, i, j, rowHeap, colHeap, grid):
        actualDis = None
        if rowElement is not None and colElement is not None:
            actualDis = min(rowElement.step, colElement.step) + 1
        elif rowElement is not None:
            actualDis = rowElement.step + 1
        elif colElement is not None:
            actualDis = colElement.step + 1
        if actualDis is not None:
            heapq.heappush(rowHeap, Node(actualDis, j + grid[i][j]))
            heapq.heappush(colHeap, Node(actualDis, i + grid[i][j]))
        return actualDis

    def getRowElement(self, rowHeap, i, j):
        while len(rowHeap) > 0:
            element = rowHeap[0]
            if element.k < j:
                heapq.heappop(rowHeap)
            else:
                return element
        return None

    def getColElement(self, colHeap, i, j):
        while len(colHeap) > 0:
            element = colHeap[0]
            if element.k < i:
                heapq.heappop(colHeap)
            else:
                return element
        return None

    def minimumVisitedCells(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        if m == 1 and n == 1:
            return 1

        rowHeaps, colHeaps = [[] for _ in range(m)], [[] for _ in range(n)]

        heapq.heappush(rowHeaps[0], Node(1, grid[0][0]))
        heapq.heappush(colHeaps[0], Node(1, grid[0][0]))

        for i in range(m):
            for j in range(n):
                if i == 0 and j == 0:
                    continue
                else:
                    rowHeap, colHeap = rowHeaps[i], colHeaps[j]
                    rowElement, colElement = self.getRowElement(rowHeap, i, j), self.getColElement(colHeap, i, j)
                    actualDis = self.getActualElement(rowElement, colElement, i, j, rowHeap, colHeap, grid)
                    if i == m - 1 and j == n - 1:
                        return actualDis if actualDis else -1
        return -1
3174