[c++] ||  Dijkstra || One of the most Understable & Simple solution
\nclass Solution {\n    vector<pair<int,int>> moves={{1,0},{0,1},{-1,0},{0,-1}};\n    vector<vector<int>> dp;\n    bool check(int x,int y,int n,int m){\n       
```\nclass Solution {\n    vector<pair<int,int>> moves={{1,0},{0,1},{-1,0},{0,-1}};\n    vector<vector<int>> dp;\n    bool check(int x,int y,int n,int m){\n        if(x>=0 && x<n && y>=0 && y<m)return true;\n        return false;\n    }\n \npublic:\n    int minimumTime(vector<vector<int>>& grid) {\n      int n=grid.size();\n        int m=grid[0].size();\n        dp.assign(n+1,vector<int>(m+1,INT_MAX));\n      \n        set<pair<int,pair<int,int>>> st;\n        st.insert({0,{0,0}});\n       \n        if(grid[1][0]>1 && grid[0][1]>1)return -1;\n        while(!st.empty()){\n            auto it=*st.begin();\n            int px=it.second.first;\n            int py=it.second.second;\n            int d=it.first;\n            st.erase(it);\n            for(int i=0;i<4;i++){\n                int x=px+moves[i].first;\n                int y=py+moves[i].second;\n                if(!check(x,y,n,m))continue;\n                 if(d+1>=grid[x][y]){\n                   if(dp[x][y]>d+1){  st.insert({d+1,{x,y}}); dp[x][y]=d+1; }\n                 }\n                else{\n                    if(d%2==(grid[x][y])%2){\n                      if(dp[x][y]>grid[x][y]+1){  st.insert({grid[x][y]+1,{x,y}}); dp[x][y]=grid[x][y]+1;}\n                            \n                    }\n                    else if(d%2!=(grid[x][y])%2){\n                      if(dp[x][y]>grid[x][y]){   st.insert({grid[x][y],{x,y}});dp[x][y]=grid[x][y]; }\n                    }\n                }\n                }\n            }\n        \n        if(dp[n-1][m-1]==INT_MAX){\n            return -1;\n        }\n        else return dp[n-1][m-1];\n        \n    }\n};\n```
['Graph']