PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000431. Khám phá hang động (Cave Exploration)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Khám phá hang động (Cave Exploration)

Cave Exploration

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

During the rainy season, a goldfish named Orange is trapped at the bottom of a cave in Thailand. The water rises by exactly one meter every hour. Orange can swim instantaneously between adjacent submerged locations, but cannot cross rocks protruding above the water.

The cave is an N×NN\times N grid whose rows and columns are numbered from 00 to N−1N-1. The rock at cell (i,j)(i,j) is hi,jh_{i,j} meters high. A cell is submerged when the water level is at least one meter above its rock height. Initially the water is one meter high, and Orange is at cell (0,0)(0,0), whose rock height is zero.

Orange can swim up, down, left or right to an adjacent cell if both cells are submerged. Diagonal moves and moves outside the grid are forbidden. The only exit is cell (N−1,N−1)(N-1,N-1).

Find the minimum number of hours before Orange can swim from the starting cell to the exit. Swimming takes no time, so only the waiting time for the rising water matters.

Input

The first line contains an integer NN, the grid size.

The next NN lines each contain NN integers. Line i+1i+1 of this block contains hi,0,hi,1,…,hi,N−1h_{i,0},h_{i,1},\ldots,h_{i,N-1}, the rock heights in row ii (0≤i<N0\le i<N).

Output

Print a single integer: the minimum number of hours before Orange can reach the exit.

Subtasks

  • Subtask 1 (20 points): 2≤N≤1002\le N\le100; 0≤hi,j≤1000\le h_{i,j}\le100 for all 0≤i,j<N0\le i,j<N; h0,0=0h_{0,0}=0.
  • Subtask 2 (30 points): 2≤N≤202\le N\le20; 0≤hi,j≤1080\le h_{i,j}\le10^8 for all 0≤i,j<N0\le i,j<N; h0,0=0h_{0,0}=0.
  • Subtask 3 (50 points): 2≤N≤1002\le N\le100; 0≤hi,j≤1080\le h_{i,j}\le10^8 for all 0≤i,j<N0\le i,j<N; h0,0=0h_{0,0}=0.

Examples

Input

2
0 3
2 4

Output

4

Explanation

The grid has two rows and two columns. Orange starts at (0,0)(0,0), of height zero, and the exit (1,1)(1,1) has height four. After three hours the water level is four meters, so the exit is not submerged. After four hours the water level is five meters, submerging all four cells. Orange can swim through (1,0)(1,0), whose rock is two meters high, to (1,1)(1,1). Earlier arrival is impossible, so the answer is four.

Input

5
0 2 3 10 4
10 23 2 21 12
11 10 12 12 16
12 12 18 10 10
10 10 10 11 10

Output

12

Explanation

After eleven hours the water level is twelve meters. The cells with rock heights at most eleven meters that Orange can reach are

$$(0,0),(0,1),(0,2),(0,3),(0,4),(1,0),(1,2),(2,0),(2,1).$$

Every adjacent cell outside this set has rock height at least twelve meters or lies outside the grid. The exit (4,4)(4,4) is not yet reachable.

After twelve hours the water level is thirteen meters. Orange can swim down column zero to row four, then right to column four. The rock heights along this route are 0,10,11,12,10,10,10,11,100,10,11,12,10,10,10,11,10. The largest is twelve meters, so every cell on this route is submerged. The answer is twelve.