시간 제한메모리 제한제출정답맞힌 사람정답 비율
7 초 512 MB114535027827.634%

문제

The Chilean Andes have become increasingly popular as a destination for backpacking and hiking. Many parts of the Andes are quite remote and thus dangerous. Because of this, the Ministry of Tourism wants to help travelers plan their trips. In particular, the travelers need to know how high they will have to climb during their journey, as this information will help them decide which equipment they need to bring. The Ministry has tasked you to provide the aspiring mountaineers with this data.

You are given a topographic map of a part of the Andes, represented as a two-dimensional grid of height values, as well as the list of origins and destinations. Mountaineers can move from each grid cell to any of the four adjacent cells. For each mountaineer find the minimal height that they must be able to reach in order to complete their journey

입력

The input consists of:

  • one line with three integers m, n and q (1 ≤ m, n ≤ 500, 1 ≤ q ≤ 105), where m is the number of rows, n is the number of columns, and q is the number of mountaineers;
  • m lines, each with n integers h1, . . . , hn (1 ≤ hi ≤ 106), the height values in the map;
  • q lines, each with four integers x1, y1, x2, y2 (1 ≤ x1, x2 ≤ m, 1 ≤ y1, y2 ≤ n), describing a mountaineer who wants to trek from (x1, y1) to (x2, y2).

The top left cell of the grid has coordinates (1, 1) and the bottom right cell has coordinates (m, n).

출력

Output q integers, the minimal height for each mountaineer, in the same order as in the input.

예제 입력 1

3 5 3
1 3 2 1 3
2 4 5 4 4
2 1 3 2 2
1 1 3 2
2 4 2 2
1 4 3 4

예제 출력 1

2
4
3