Simple bfs solution
\njava []\nclass Solution {\n    public int minTimeToReach(int[][] m) {\n          PriorityQueue<int[]>pq=new PriorityQueue<>((a,b)->a[2]-b[2]);\n        pq.off
\n```java []\nclass Solution {\n    public int minTimeToReach(int[][] m) {\n          PriorityQueue<int[]>pq=new PriorityQueue<>((a,b)->a[2]-b[2]);\n        pq.offer(new int[]{0,0,0,0});\n        int[][]vis=new int[m.length][m[0].length];\n        int[]dr=new int[]{0,1,0,-1};\n        int[]dc=new int[]{1,0,-1,0};\n        vis[0][0]=1;\n        while(pq.size()>0){\n          int[]x=pq.poll(); \n          if(x[0]==m.length-1&&x[1]==m[0].length-1)return x[2];\n          for(int i=0;i<4;i++){\n            int nr=dr[i]+x[0];\n            int nc=dc[i]+x[1];\n            if(nr<m.length&&nr>=0&&nc>=0&&nc<m[0].length&&vis[nr][nc]==0){\n               vis[nr][nc]=1;\n               if(x[3]==1){\n                pq.offer(new int[]{nr,nc,Math.max(x[2],m[nr][nc])+2,2});\n               }\n               else{\n                   pq.offer(new int[]{nr,nc,Math.max(x[2],m[nr][nc])+1,1});    \n               }\n          //      if(nr==m.length-1&&nc==m[0].length-1)return ,Math.max(x[2],m[nr][nc])+1;\n            }\n          } \n        }\n        return -1;\n    }\n}\n```
['Java']