Problem: Owl Sonya gave a huge lake puzzle of size n × m to hedgehog Filya as a birthday present. Friends immediately started to assemble the puzzle, but some parts of it turned out to be empty — there was no picture on them. Parts with picture on it are denoted by 1, while empty parts are denoted by 0. Rows of the puzzle are numbered from top to bottom with integers from 1 to n, while columns are numbered from left to right with integers from 1 to m.

Animals decided to complete the picture and play with it, as it might be even more fun! Owl and hedgehog ask each other some queries. Each query is provided by four integers x1, y1, x2, y2 which define the rectangle, where (x1, y1) stands for the coordinates of the up left cell of the rectangle, while (x2, y2) stands for the coordinates of the bottom right cell. The answer to the query is the size of the maximum square consisting of picture parts only (only parts denoted by 1) and located fully inside the query rectangle.

Help Sonya and Filya answer t queries.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 1000) — sizes of the puzzle.

Each of the following n lines contains m integers aij. Each of them is equal to 1 if the corresponding cell contains a picture and 0 if it's empty.

Next line contains an integer t (1 ≤ t ≤ 1 000 000) — the number of queries.

Then follow t lines with queries' descriptions. Each of them contains four integers x1, y1, x2, y2 (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m) — coordinates of the up left and bottom right cells of the query rectangle.

Output

Print t lines. The i-th of them should contain the maximum size of the square consisting of 1-s and lying fully inside the query rectangle.

Example

Input

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


Output

1
1
1
2
2

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
short n, m, rmq[11][11][1005][1005], lg[1024];
int main() {
  scanf("%hi %hi", &n, &m);
  for (short i = 1; i <= n; ++i)
    for (short j = 1; j <= m; ++j) {
      int x;
      scanf("%d", &x);
      if (x)
        rmq[0][0][i][j] = min(min(rmq[0][0][i - 1][j - 1], rmq[0][0][i - 1][j]),
                              rmq[0][0][i][j - 1]) +
                          1;
    }
  for (short i = 2; i <= 1000; ++i) lg[i] = lg[i >> 1] + 1;
  for (short p = 1; (1 << p) <= n; ++p)
    for (short i = 1; i + (1 << p) - 1 <= n; ++i)
      for (short j = 1; j <= m; ++j)
        rmq[p][0][i][j] =
            max(rmq[p - 1][0][i][j], rmq[p - 1][0][i + (1 << (p - 1))][j]);
  for (short q = 1; (1 << q) <= m; ++q)
    for (short i = 1; i <= n; ++i)
      for (short j = 1; j + (1 << q) - 1 <= m; ++j)
        rmq[0][q][i][j] =
            max(rmq[0][q - 1][i][j], rmq[0][q - 1][i][j + (1 << (q - 1))]);
  for (short p = 1; (1 << p) <= n; ++p)
    for (short q = 1; (1 << q) <= m; ++q)
      for (short i = 1; i + (1 << p) - 1 <= n; ++i)
        for (short j = 1; j + (1 << q) - 1 <= m; ++j)
          rmq[p][q][i][j] = max(
              max(rmq[p - 1][q - 1][i + (1 << (p - 1))][j],
                  rmq[p - 1][q - 1][i][j + (1 << (q - 1))]),
              max(rmq[p - 1][q - 1][i][j],
                  rmq[p - 1][q - 1][i + (1 << (p - 1))][j + (1 << (q - 1))]));
  int t;
  scanf("%d", &t);
  for (; t; --t) {
    short x, y, a, b;
    scanf("%hi %hi %hi %hi", &x, &y, &a, &b);
    short st = 1, dr = min(a - x + 1, b - y + 1), poz = 0;
    while (st <= dr) {
      short mij = (st + dr) >> 1;
      short cx = x, cy = y;
      x += mij - 1;
      y += mij - 1;
      short p = lg[a - x + 1], q = lg[b - y + 1];
      short val = max(
          max(rmq[p][q][x][y], rmq[p][q][a - (1 << p) + 1][b - (1 << q) + 1]),
          max(rmq[p][q][a - (1 << p) + 1][y], rmq[p][q][x][b - (1 << q) + 1]));
      if (val >= mij)
        poz = mij, st = mij + 1;
      else
        dr = mij - 1;
      x = cx, y = cy;
    }
    printf("%hi\n", poz);
  }
  return 0;
}