Dijkstra's
Code

# Code
```cpp []
class Solution {
public:
    int minimumTime(vector<vector<int>>& grid) {
        if(grid[0][0]>0 || (grid[0][1]>1 && grid[1][0]>1)) return -1;
        int n = grid.size(), m = grid[0].size();
        vector<vector<int>> dist(n,vector<int>(m,INT_MAX));
        priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
        pq.push({0,{0,0}});
        int dir[] = {0,1,0,-1,0};
        dist[0][0]=0;
        while(!pq.empty()){
            auto[time, xy] = pq.top();
            auto[x, y]=xy;
            pq.pop();
            for(int i=0; i<4; i++){
                int nx = x+dir[i], ny = y+dir[i+1];
                if(nx>=0 && ny>=0 && nx<n && ny<m){
                    int diff = abs(grid[nx][ny]-time);
                    int newtime = diff%2==0? grid[nx][ny]+1:grid[nx][ny];
                    if(time+1>=grid[nx][ny] && dist[nx][ny]>time+1){
                        dist[nx][ny]=time+1;
                        pq.push({dist[nx][ny], {nx, ny}});
                    }else if(time+1<grid[nx][ny] && dist[nx][ny]>newtime){
                        dist[nx][ny]=newtime;
                        pq.push({dist[nx][ny], {nx, ny}});
                    }
                }
            }
        }
        return dist.back().back();

    }
};
```
['Breadth-First Search', 'Heap (Priority Queue)', 'Matrix', 'C++']