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;
int n, m;
int a[1007][1007];
int pref[1007][1007];
short RMQ[1007][1007][11][11];
int stid[1007];
int xx1, yy1, xx2, yy2;
bool check(int x) {
  int p, q;
  p = xx2 - x + 1;
  q = yy2 - x + 1;
  int id1 = stid[p - xx1 + 1];
  int id2 = stid[q - yy1 + 1];
  int len1 = (1 << id1);
  int len2 = (1 << id2);
  short mx = 0;
  mx = max(mx, RMQ[xx1][yy1][id1][id2]);
  mx = max(mx, RMQ[xx1][q - len2 + 1][id1][id2]);
  mx = max(mx, RMQ[p - len1 + 1][yy1][id1][id2]);
  mx = max(mx, RMQ[p - len1 + 1][q - len2 + 1][id1][id2]);
  int u = mx;
  return (u >= x);
}
void input() {
  scanf("%d%d", &n, &m);
  int i, j;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
      pref[i][j] =
          a[i][j] + pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
    }
  }
}
void solve() {
  int i, j, t, z;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (a[i][j] == 0) {
        RMQ[i][j][0][0] = 0;
        continue;
      }
      int l, r, mid;
      l = 1;
      r = min((n - i + 1), (m - j + 1));
      while (r - l > 3) {
        mid = (l + r) / 2;
        int val = pref[i + mid - 1][j + mid - 1] - pref[i + mid - 1][j - 1] -
                  pref[i - 1][j + mid - 1] + pref[i - 1][j - 1];
        if (val == mid * mid) {
          l = mid;
        } else {
          r = mid;
        }
      }
      while (1) {
        int val = pref[i + r - 1][j + r - 1] - pref[i + r - 1][j - 1] -
                  pref[i - 1][j + r - 1] + pref[i - 1][j - 1];
        if (val == r * r) {
          break;
        } else {
          r--;
        }
      }
      RMQ[i][j][0][0] = r;
    }
  }
  for (t = 0; t <= 9; t++) {
    for (z = 0; z <= 9; z++) {
      if (t + z == 0) {
        continue;
      }
      for (i = 1; i <= n; i++) {
        if ((i + (1 << t) - 1) > n) {
          break;
        }
        for (j = 1; j <= m; j++) {
          if ((j + (1 << z) - 1) > m) {
            break;
          }
          if (t != 0 && z == 0) {
            RMQ[i][j][t][z] = RMQ[i][j][t - 1][z];
            RMQ[i][j][t][z] =
                max(RMQ[i][j][t][z], RMQ[i + (1 << (t - 1))][j][t - 1][z]);
          } else if (t == 0 && z != 0) {
            RMQ[i][j][t][z] = RMQ[i][j][t][z - 1];
            RMQ[i][j][t][z] =
                max(RMQ[i][j][t][z], RMQ[i][j + (1 << (z - 1))][t][z - 1]);
          } else {
            RMQ[i][j][t][z] = RMQ[i][j][t - 1][z - 1];
            RMQ[i][j][t][z] =
                max(RMQ[i][j][t][z], RMQ[i + (1 << (t - 1))][j][t - 1][z]);
            RMQ[i][j][t][z] =
                max(RMQ[i][j][t][z], RMQ[i][j + (1 << (z - 1))][t][z - 1]);
            RMQ[i][j][t][z] =
                max(RMQ[i][j][t][z],
                    RMQ[i + (1 << (t - 1))][j + (1 << (z - 1))][t - 1][z - 1]);
          }
        }
      }
    }
  }
  int br = 0;
  int val = 1;
  for (i = 1; i <= 1000; i++) {
    while (2 * val < i) {
      br++;
      val *= 2;
    }
    stid[i] = br;
  }
  int q;
  scanf("%d", &q);
  while (q != 0) {
    q--;
    scanf("%d%d%d%d", &xx1, &yy1, &xx2, &yy2);
    int l, r, mid;
    l = 0;
    r = min(xx2 - xx1 + 1, yy2 - yy1 + 1);
    while (r - l > 3) {
      int mid = (l + r) / 2;
      if (check(mid) == true) {
        l = mid;
      } else {
        r = mid;
      }
    }
    while (check(r) == false) {
      r--;
    }
    if (r < 0) {
      r = 0;
    }
    printf("%d\n", r);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  input();
  solve();
  return 0;
}