Python | Dijkstra | Simple Solution
Code\n\nfrom heapq import heappush, heappop\nclass Solution:\n    def minimumTime(self, grid: List[List[int]]) -> int:\n        m, n = len(grid), len(grid[0])\n
# Code\n```\nfrom heapq import heappush, heappop\nclass Solution:\n    def minimumTime(self, grid: List[List[int]]) -> int:\n        m, n = len(grid), len(grid[0])\n        if grid[0][1] > 1 and grid[1][0] > 1: \n            return -1\n        heap = [(0,0,0)]\n        visited = set()\n        while heap:\n            d, i, j = heappop(heap)\n            if (i, j) in visited: continue\n            visited.add((i,j))\n            if (i, j) == (m-1, n-1): return d\n            for di, dj in [[-1,0], [1,0], [0,-1], [0,1]]:\n                if 0 <= i + di < m and 0 <= j + dj < n and (i + di, j + dj) not in visited:\n                    t = grid[i+di][j+dj]\n                    gap = max(t-(d+1), 0)\n                    heappush(heap, (d + 1 + gap + (gap%2), i + di, j + dj))\n```
['Python3']