#C5183. Maximum in Subgrid

    ID: 48804 Type: Default 1000ms 256MiB

Maximum in Subgrid

Maximum in Subgrid

You are given a grid of integers with n rows and m columns. You will also be provided with q queries. Each query specifies a subgrid of the original grid using four integers r1, c1, r2, c2 representing the top‐left and bottom‐right corners respectively (using 1-indexed coordinates). For each query, your task is to determine the maximum value within the subgrid.

The subgrid can be mathematically described as:

$$\{grid[i][j] \mid r_1 \leq i \leq r_2,\; c_1 \leq j \leq c_2\} $$

Print the maximum for each query on a new line.

inputFormat

The input is given from standard input (stdin) with the following format:

  1. The first line contains two integers n and m, denoting the number of rows and columns in the grid.
  2. The next n lines each contain m integers, representing the rows of the grid.
  3. The next line contains an integer q, the number of queries.
  4. Each of the following q lines contains four integers r1, c1, r2, c2 specifying a subgrid.

All indices use 1-based indexing.

outputFormat

For each query, output a single line with the maximum integer present in the specified subgrid.

## sample
3 3
1 2 3
4 5 6
7 8 9
1
1 1 2 2
5

</p>