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;
const int P = 1e9 + 7;
int add(int a, int b) {
  if ((a += b) >= P) a -= P;
  return a;
}
int sub(int a, int b) {
  if ((a -= b) < 0) a += P;
  return a;
}
int mul(int a, int b) { return 1ll * a * b % P; }
int kpow(int a, int b) {
  int r = 1;
  for (; b; b >>= 1, a = mul(a, a)) {
    if (b & 1) r = mul(r, a);
  }
  return r;
}
const int N = 1010;
const int LGN = (31 - __builtin_clz(N)) + 1;
short f[N][N], a[N][N];
int n, m, q, l, r, mid, a1, b1, a2, b2, ans;
struct RMQ_2D {
  short v[LGN][LGN][N][N];
  void init(int n, int m, short a[N][N]) {
    const int lgn = (31 - __builtin_clz(n));
    const int lgm = (31 - __builtin_clz(m));
    for (int i = (0); i < (lgn + 1); i++)
      for (int j = (0); j < (lgm + 1); j++) {
        int ux = n - (1 << i);
        int uy = m - (1 << j);
        for (int x = (0); x < (ux + 1); x++)
          for (int y = (0); y < (uy + 1); y++) {
            auto &me = v[i][j][x][y];
            if (i == 0 && j == 0)
              me = a[x][y];
            else if (i == 0)
              me = max(v[i][j - 1][x][y], v[i][j - 1][x][y + (1 << (j - 1))]);
            else
              me = max(v[i - 1][j][x][y], v[i - 1][j][x + (1 << (i - 1))][y]);
          }
      }
  }
  int ask(int x1, int y1, int x2, int y2) {
    const int lx = (31 - __builtin_clz(x2 - x1 + 1));
    const int ly = (31 - __builtin_clz(y2 - y1 + 1));
    auto *me = v[lx][ly];
    x2 -= (1 << lx) - 1;
    y2 -= (1 << ly) - 1;
    return max(max(me[x1][y1], me[x1][y2]), max(me[x2][y1], me[x2][y2]));
  }
} rmq;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cin >> n >> m;
  for (int i = (1); i < (n + 1); i++)
    for (int j = (1); j < (m + 1); j++) cin >> a[i][j];
  for (int i = (1); i < (n + 1); i++)
    for (int j = (1); j < (m + 1); j++)
      if (a[i][j] == 0)
        f[i][j] = 0;
      else
        f[i][j] = min(min(f[i - 1][j], f[i][j - 1]), f[i - 1][j - 1]) + 1;
  rmq.init(n + 1, m + 1, f);
  cin >> q;
  for (int i = (1); i < (q + 1); i++) {
    cin >> a1 >> b1 >> a2 >> b2;
    l = 0;
    r = min(a2 - a1 + 1, b2 - b1 + 1);
    ans = 0;
    while (l <= r) {
      mid = l + r >> 1;
      if (rmq.ask(a1 + mid - 1, b1 + mid - 1, a2, b2) >= mid) {
        ans = max(ans, mid);
        l = mid + 1;
      } else
        r = mid - 1;
    }
    cout << ans << "\n";
  }
  return 0;
}