You are given an n x n integer matrix grid where each value grid[i][j] represents the elevation at that point (0,0). The rain starts at time = 0. At time t, the depth of the water everywhere is t. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most t. Find the least time t at which you can swim from the top left (0, 0) to the bottom right (n-1, n-1).
Example 1:
Input: 2 0 2 1 3 Output: 3
Example 2:
Input: 5 0 1 2 3 4 24 23 22 21 5 12 13 14 15 16 11 17 18 19 20 10 9 8 7 6 Output: 16
Constraints:
- n == grid.length == grid[i].length
- 1 ≤ n ≤ 50
- 0 ≤ grid[i][j] < n²
- All values are unique
Input format: First line: n. Then n lines with n space-separated integers.
Output format: Minimum time.