Grapevine

Time limit1sMemory limit128 MB

Summary
Given a monotone matrix of heights and height-interval queries, find for each query the largest square submatrix whose heights all fall in the interval.
Level

Hard8 of 10

Topics
Binary search, Dynamic programming, Prefix sum, Matrix
Solved
No attempts yet

Problem

In Quadradonia, all rural properties are square, all have the same area, all are perfectly flat, and all have their sides aligned with the North-South and West-East axes.

Because the properties are flat, the hills of Quadradonia look like a series of enormous staircase steps of differing heights. On one particular mountain there is an interesting rectangular region of N×MN \times M properties. Starting from any property and moving from West to East, the heights are non-decreasing. Likewise, starting from any property and moving from North to South, the heights are also non-decreasing.

A large wine company in Quadradonia wants to rent some properties in this region to grow wine grapes. The company is interested in special grape varieties that are productive only when grown on properties whose heights lie within a certain interval. That is, the company wants to rent properties whose heights are at least a given altitude LL and at most a given altitude UU. To make harvesting easier, the rented properties must form a contiguous area, and, because everyone in Quadradonia loves squares, that area must be a square.

The company has not yet decided which variety it will grow, so it has a list of queries, one per grape variety, each describing a height interval.

Write a program that, given the description of the rectangular region of interest and a list of height-interval queries, determines for each query the largest possible side, measured in number of properties, of a contiguous square area whose heights all lie within the specified interval. For example, in a 4×54 \times 5 region of interest, several different squares may satisfy different height intervals.

Input

The input contains several test cases. The first line of each test case contains two integers NN and MM separated by a single space, giving respectively the number of properties in the North-South direction (1≤N≤5001 \le N \le 500) and in the West-East direction (1≤M≤5001 \le M \le 500). Each of the next NN lines contains MM integers Hi,jH_{i,j} separated by single spaces, giving the heights of the properties (for 1≤i≤N1 \le i \le N and 1≤j≤M1 \le j \le M, 0≤Hi,j≤1050 \le H_{i,j} \le 10^5; moreover Hi−1,j≤Hi,jH_{i-1,j} \le H_{i,j} and Hi,j−1≤Hi,jH_{i,j-1} \le H_{i,j}). The next line contains an integer QQ, the number of queries (1≤Q≤1041 \le Q \le 10^4). Each of the next QQ lines contains two integers LL and UU separated by a single space, describing one height interval (0≤L≤U≤1050 \le L \le U \le 10^5); the heights of the rented properties must be at least LL and at most UU.

The last test case is followed by a line containing two zeros separated by a single space, which must not be processed.

Output

For each test case, print Q+1Q + 1 lines. Each of the first QQ lines must contain a single integer: the largest side, in number of properties, of a contiguous square area whose heights all lie within the interval of the corresponding query (print 00 if no such square exists). The last line printed for each test case is a separator consisting of a single hyphen character '-'.

Examples3

  1. Example 1

    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
    4 4
    1 7 9 11
    5 8 10 12
    7 10 15 17
    11 19 30 41
    4
    6 20
    7 9
    10 10
    13 14
    0 0
    
    Expected output
    3
    2
    4
    -
    3
    1
    1
    0
    -
    
  2. Example 2

    Input
    1 1
    5
    2
    5 5
    0 4
    0 0
    
    Expected output
    1
    0
    -
    
  3. Example 3

    Input
    3 3
    0 0 0
    0 0 0
    0 0 0
    3
    0 0
    0 100000
    1 5
    0 0
    
    Expected output
    3
    3
    0
    -