n × m grid of rooms. For each room you know opens[i][j], the moment (in seconds) from which you are allowed to **start moving into it** from a neighbouring room.
You stand in room (0, 0) at time t = 0. In one move you can step into a room that shares a wall with yours (up, down, left or right), and a move takes exactly one second. You may wait in place as long as you like.
Return the earliest moment you can be in room (n - 1, m - 1).opens=[[0, 3], [1, 9]] → 10
Head down to (1, 0) at time 1 and arrive at 2. The door to (1, 1) opens only at 9, so we arrive at 10. Going through (0, 1) is no better: we get there at 4 and still wait until 9.
opens=[[0, 0, 0], [0, 0, 0], [0, 0, 0]] → 4
Every door is open from the start, so the time equals the shortest path: 4 steps.
opens=[[0, 5, 0], [2, 7, 1]] → 8
opens=[[0, 3], [1, 9]]
10
Run checks the first 3 tests in your browser. Submit checks all 89 tests on the server, hidden ones included.