pure bfs, no heap, max(grid[i][j])+mn
Approach\n1) as regular BFS, move 1 level at a time and record adjacent cells as new level\n2) the difference is, if adjacent cell\'s required time > current ti
# Approach\n1) as regular BFS, move 1 level at a time and record adjacent cells as new level\n2) the difference is, if `adjacent cell\'s required time > current time + 1`, meaning we need to move back-and-forth to reach this cell, so put them in future level to be processed\n3) for 2), one caveat is that when steps between adjacent cell\'s time and current time is odd, it can move back-and-forth till exact adjacent cell time to reach it,\nbut when it is even, it need 1 extra move to reach the adjacent cell\n\ne.g. \n```\nt=0             t[5] = (1,0)\n  0  1  2  3\n0[0*,1a,3 ,2 ]\n1[5f,1 ,2 ,5 ]\n2[4 ,3 ,8 ,6 ]\n\nt=1             t[4] = (2,2)\n  0  1  2  3    t[5] = (1,0)\n0[0-,1*,3f,2 ]\n1[5f,1a,2 ,5 ]\n2[4 ,3 ,8 ,6 ]\n\nt=2             t[4] = (2,2)\n  0  1  2  3    t[5] = (1,0)\n0[0-,1-,3f,2 ]\n1[5f,1*,2a,5 ]\n2[4 ,3a,8 ,6 ]\n\nt=3             t[4] = (2,2)\n  0  1  2  3    t[5] = (1,0)\n0[0-,1-,3f,2 ]  t[6] = (1,3)\n1[5f,1-,2*,5f]  t[8] = (2,2)\n2[4a,3*,8f,6 ]\n\nt=4             t[5] = (1,0)\n  0  1  2  3    t[6] = (1,3)\n0[0-,1-,3*,2a]  t[8] = (2,2)\n1[5f,1-,2-,5f]\n2[4*,3-,8f,6 ]\n\nt=5             \n  0  1  2  3    t[6] = (1,3)\n0[0-,1-,3-,2*]  t[8] = (2,2)\n1[5*,1-,2-,5f]\n2[4-,3-,8f,6 ]\n\nt=6             \n  0  1  2  3    \n0[0-,1-,3-,2-]  t[8] = (2,2)\n1[5-,1-,2-,5*]\n2[4-,3-,8f,6a]\n\nt=7             \n  0  1  2  3    \n0[0-,1-,3-,2-]  t[8] = (2,2)\n1[5-,1-,2-,5-]\n2[4-,3-,8f,6*]\n```\n\n# Complexity\n- Time complexity: O(max(grid[i][j])+mn)\n- Space complexity: O(m*n)\n\n\n# Code\n```python\nclass Solution:\n    def minimumTime(self, grid: List[List[int]]) -> int:\n        R, C = 0, 1\n        nr, nc = len(grid), len(grid[0])\n\n        def get_nx(r, c):\n            for r_nx, c_nx in ((r+1,c),(r-1,c),(r,c+1),(r,c-1)):\n                if 0 <= r_nx <= nr-1 and 0 <= c_nx <= nc-1:\n                    yield r_nx, c_nx\n\n        # main\n        if grid[0][1] > 1 and grid[1][0] > 1:\n            return -1\n        start, end = (0, 0), (nr-1, nc-1)\n        t_to_cells = defaultdict(set)\n        t = 0\n        t_to_cells[t].add(start)\n        visited = {start}\n        while True:\n            for r, c in t_to_cells[t]:\n                for r_nx, c_nx in get_nx(r, c):\n                    if (r_nx, c_nx) in visited:\n                        continue\n                    if grid[r_nx][c_nx] > t+1:\n                        t_diff = grid[r_nx][c_nx] - t\n                        t_nx = t + t_diff if t_diff % 2 == 1 else t + t_diff + 1\n                    else:\n                        t_nx = t+1\n                    if (r_nx, c_nx) == end:\n                        return t_nx\n                    t_to_cells[t_nx].add((r_nx, c_nx))\n                    visited.add((r_nx, c_nx))\n            t += 1\n\n```
['Breadth-First Search', 'Python3']