easiest bfs & dijkstra soln, java code.
Intuition & Approachcase1: when on start cell, we have two options to explore with, right or down, and time taken to reach them is 1s, but if their minTime > 1,
# Intuition & Approach
<!-- Describe your first thoughts on how to solve this problem. -->
case1: when on start cell, we have two options to explore with, right or down, and time taken to reach them is 1s, but if their minTime > 1, we can't go anywhere, we stuck, we return -1
case2: when we stuck on some other cell, where its new neighbors have minTime > currTime(whatever time we have) we may have to revisit on old path to pass out time until we can explore new neighbor, point is once we are out of first cell, we can always reach endpoint, now its about reaching in minimum time as possible, so for that use pq

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
bfs: O(mn)
pq: one cell may add new 4 cells, (if all 4 are still unvisited), over all all cells will be added once at max, so worst case TC = O(mn log(mn)) 
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
vis array: O(mn)
pq: O(mn)
# Code
```java []
class Solution {
    public int minimumTime(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int time = 0;

        if(grid[0][1] > 1 && grid[1][0] > 1) return -1;

        boolean[][] vis = new boolean[m][n];
        PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> a[2] - b[2]);

        vis[0][0] = true;
        q.add(new int[]{0, 0, time});

        int[] delrow = {-1, 0, 1, 0};
        int[] delcol = {0, 1, 0, -1};

        while(!q.isEmpty()) {
            int[] top = q.poll();
            int currRow = top[0];
            int currCol = top[1];
            int currTime = top[2];

            if(currRow == m-1 && currCol == n-1) return currTime;

            for(int i=0;i<4;i++) {
                int nrow = currRow + delrow[i];
                int ncol = currCol + delcol[i];
                if(nrow >= 0 && nrow < m && ncol >= 0 && ncol < n && !vis[nrow][ncol]) {
                    if(currTime + 1 >= grid[nrow][ncol]) {
                        q.add(new int[]{nrow, ncol, currTime+1});
                    } else {
                        int diff = grid[nrow][ncol];
                        if(((diff - currTime) & 1) == 1) 
                            q.add(new int[]{nrow, ncol, diff});
                        else q.add(new int[]{nrow, ncol, diff+1});
                    }
                    vis[nrow][ncol] = true;
                }
            }
        }

        return -1;
    }
}
```
['Array', 'Breadth-First Search', 'Graph', 'Heap (Priority Queue)', 'Matrix', 'Shortest Path', 'Java']