via Priority Queue
Complexity\n- Time complexity:\nO((n * m).log(n * m))\n\n- Space complexity:\nO(n * m)\n\n# Code\ncsharp []\npublic class Solution {\n    public int MinTimeToRe
# Complexity\n- Time complexity:\n$$O((n * m).log(n * m))$$\n\n- Space complexity:\n$$O(n * m)$$\n\n# Code\n```csharp []\npublic class Solution {\n    public int MinTimeToReach(int[][] moveTime) {\n        int n = moveTime.Length;\n        int m = moveTime[0].Length;\n\n        HashSet<(int, int)> directions = new HashSet<(int, int)>() { (1, 0), (-1, 0), (0, 1), (0, -1) };\n        PriorityQueue<(int, int, int, int), int> queue = new PriorityQueue<(int, int, int, int), int>();\n        HashSet<(int, int)> visited = new HashSet<(int, int)>();\n\n        queue.Enqueue((0, 0, 0, 0), 0);\n        visited.Add((0, 0));\n\n        while (queue.Count > 0) {\n            var (x, y, time, last) = queue.Dequeue();\n            \n            if (x == n - 1 && y == m - 1) {\n                return time;\n            }\n\n            if (last == 1) {\n                last = 2;\n            }\n            else {\n                last = 1;\n            }\n\n            foreach (var (dx, dy) in directions) {\n                int newX = x + dx;\n                int newY = y + dy;\n\n                if (newX < 0 || newX >= n || newY < 0 || newY >= m) {\n                    continue;\n                }\n\n                if (visited.Contains((newX, newY))) {\n                    continue;\n                }\n\n                int next = Math.Max(time, moveTime[newX][newY]) + last;\n\n                queue.Enqueue((newX, newY, next, last), next);\n                visited.Add((newX, newY));\n            }\n        }\n\n        return -1;\n    }\n}\n```
['Graph', 'Heap (Priority Queue)', 'Shortest Path', 'C#']