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;
inline long long rd() {
  long long x = 0;
  short ch = getchar(), f = 1;
  while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void rt(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10)
    rt(x / 10), putchar(x % 10 + '0');
  else
    putchar(x + '0');
}
const short maxn = 1005;
short n, m, size;
short a[10][10][maxn][maxn], lg[maxn], pw[10];
short querymax(short x1, short sto_szj, short x2, short sto_rxd) {
  short lx = lg[x2 - x1 + 1], ly = lg[sto_rxd - sto_szj + 1];
  return max(max(a[lx][ly][x2][sto_rxd],
                 a[lx][ly][x1 + pw[lx] - 1][sto_szj + pw[ly] - 1]),
             max(a[lx][ly][x2][sto_szj + pw[ly] - 1],
                 a[lx][ly][x1 + pw[lx] - 1][sto_rxd]));
}
int main() {
  n = rd(), m = rd(), size = max(n, m);
  lg[1] = 0;
  for (short i = 2; i <= (short)size; i++) lg[i] = lg[i >> 1] + 1;
  pw[0] = 1;
  for (short i = 1; i <= (short)lg[size]; i++) pw[i] = pw[i - 1] << 1;
  for (short i = 1; i <= (short)n; i++)
    for (short j = 1; j <= (short)m; j++) a[0][0][i][j] = rd();
  for (short i = 2; i <= (short)n; i++)
    for (short j = 2; j <= (short)m; j++)
      if (a[0][0][i][j])
        a[0][0][i][j] = min(min(a[0][0][i - 1][j], a[0][0][i][j - 1]),
                            a[0][0][i - 1][j - 1]) +
                        1;
  for (short k = 1; k <= (short)lg[n]; k++)
    for (short i = pw[k]; i <= (short)n; i++)
      for (short j = 1; j <= (short)m; j++)
        a[k][0][i][j] = max(a[k - 1][0][i][j], a[k - 1][0][i - pw[k - 1]][j]);
  for (short k = 1; k <= (short)lg[m]; k++)
    for (short i = 1; i <= (short)n; i++)
      for (short j = pw[k]; j <= (short)m; j++)
        a[0][k][i][j] = max(a[0][k - 1][i][j], a[0][k - 1][i][j - pw[k - 1]]);
  for (short k = 1; k <= (short)lg[n]; k++)
    for (short l = 1; l <= (short)lg[m]; l++)
      for (short i = pw[k]; i <= (short)n; i++)
        for (short j = pw[l]; j <= (short)m; j++)
          a[k][l][i][j] =
              max(max(a[k - 1][l - 1][i][j], a[k - 1][l - 1][i][j - pw[l - 1]]),
                  max(a[k - 1][l - 1][i - pw[k - 1]][j],
                      a[k - 1][l - 1][i - pw[k - 1]][j - pw[l - 1]]));
  for (int q = rd(); q--;) {
    short x1 = rd(), sto_szj = rd(), x2 = rd(), sto_rxd = rd();
    short l = 0, r = min(x2 - x1 + 1, sto_rxd - sto_szj + 1);
    while (l < r) {
      short mid = l + r + 1 >> 1;
      if (querymax(x1 + mid - 1, sto_szj + mid - 1, x2, sto_rxd) >= mid)
        l = mid;
      else
        r = mid - 1;
    }
    rt(l), putchar('\n');
  }
}