<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 ST:
    def __init__(self,n,init=inf):
        self.trees = [init]*(1<<(n.bit_length()+1))
    def pushup(self,o):
        self.trees[o] = min(self.trees[2*o],self.trees[2*o+1])
    def update(self,o,l,r,i,val):
        if l==r==i:
            self.trees[o]=val
            return 
        m = (l+r)//2
        if i<=m:
            self.update(2*o,l,m,i,val)
        else:
            self.update(2*o+1,m+1,r,i,val)
        self.pushup(o)
    def query(self,o,l,r,L,R):
        if L<=l<=r<=R:
            return self.trees[o]
        m = (l+r)//2
        if R<=m: return self.query(2*o,l,m,L,R)
        elif L>=m+1: return self.query(2*o+1,m+1,r,L,R)
        else: return min(self.query(2*o,l,m,L,R),self.query(2*o+1,m+1,r,L,R))

class Solution:
    def minimumVisitedCells(self, g: List[List[int]]) -> int:
        m,n = len(g),len(g[0])
        frow =[ST(n) for _ in range(m)]
        fcol = [ST(m) for _ in range(n)]
        frow[-1].update(1,0,n-1,n-1,0)
        fcol[-1].update(1,0,m-1,m-1,0)

        for i in range(m-1,-1,-1):
            for j in range(n-1,-1,-1):
                up = min(j+g[i][j],n-1)
                u = frow[i].query(1,0,n-1,j,up)
                up = min(i+g[i][j],m-1)
                v = fcol[j].query(1,0,m-1,i,up)
                x = 1+min(u,v)
                frow[i].update(1,0,n-1,j,x)
                fcol[j].update(1,0,m-1,i,x)
    
        # f = [[inf]*n for _ in range(m)]
        # f[m-1][n-1]=0
        # for i in range(m-1,-1,-1):
        #     for j in range(n-1,-1,-1):
        #         up = min(j+g[i][j],n-1)
        #         u = min(f[i][k] for k in range(j,up+1))
        #         up = min(i+g[i][j],m-1)
        #         v = min(f[k][j] for k in range(i,up+1))
        #         f[i][j] = min(u,v)+1
        ans = frow[0].query(1,0,n-1,0,0) if frow[0].query(1,0,n-1,0,0)<inf else -1


        return ans
        
7810