#BS0000013. Vườn nho (Grapevine)
Vườn nho (Grapevine)
Grapevine
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an height matrix. Values are non-decreasing from left to right in each row and from top to bottom in each column.
For each query interval , find the largest side length of a contiguous square submatrix whose every value satisfies .
Input
The first line contains and .
The next lines contain the matrix.
The next line contains .
The next lines contain and .
Output
For each query, print the maximum valid square side length. Print 0 if no cell is valid.
Subtasks
- Subtask 1 — 20%: , .
- Subtask 2 — 30%: , .
- Subtask 3 — 50%: , .
All matrix values and query bounds are between and .
Examples
Input
4 5
13 21 25 33 34
16 21 33 35 35
16 33 33 45 50
23 51 66 83 93
3
22 90
33 35
20 100
Output
3
2
4
Explanation
For , a valid square of side exists, but no valid square of side exists.