c++ solution
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int minTimeToReach(vector<vector<int>>& mat) {
        int r = mat.size();
        int c = mat[0].size();

        vector<vector<int>> d(r, vector<int>(c, INT_MAX));

        priority_queue<pair<int, pair<int, int>>,
                       vector<pair<int, pair<int, int>>>,
                       greater<pair<int, pair<int, int>>>>
            q;

        vector<vector<int>> second(r, vector<int>(c, 0));

        q.push({0, {0, 0}}); // time, row, column;
        d[0][0] = 0;

        while (!q.empty()) {
            int t = q.top().first;
            int i = q.top().second.first;
            int j = q.top().second.second;
            q.pop();
            int val = second[i][j] + 1;

            if (i > 0 && max(t, mat[i - 1][j]) + val < d[i - 1][j]) {
                d[i - 1][j] = max(t, mat[i - 1][j]) + val;
                q.push({d[i - 1][j], {i - 1, j}});
                second[i - 1][j] = !(val - 1);
            }
            if (j < c - 1 && max(t, mat[i][j + 1]) + val < d[i][j + 1]) {
                d[i][j + 1] = max(t, mat[i][j + 1]) + val;
                q.push({d[i][j + 1], {i, j + 1}});
                second[i][j + 1] = !(val - 1);
            }
            if (i < r - 1 && max(t, mat[i + 1][j]) + val < d[i + 1][j]) {
                d[i + 1][j] = max(t, mat[i + 1][j]) + val;
                q.push({d[i + 1][j], {i + 1, j}});
                second[i + 1][j] = !(val - 1);
            }
            if (j > 0 && max(t, mat[i][j - 1]) + val < d[i][j - 1]) {
                d[i][j - 1] = max(t, mat[i][j - 1]) + val;
                q.push({d[i][j - 1], {i, j - 1}});
                second[i][j - 1] = !(val - 1);
            }
        }

        return d[r - 1][c - 1];
    }
};
```
['C++']