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>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
typedef double LD;
const int N = 1000 + 5;
const int M = N * N;
int n, m;
int a[N][N];
int Q;
std::pair<int, int> u[M], v[M];
int V[N][N];
int L[N][N];
int U[N][N];
int res[M];
std::vector<int> r[N][N];
const int H = 11;
int ma[H][N][N];
int fen[H][N][N];
int he[N];
void update(int h, int j, int i, int val) {
  for (; i <= n; i += (i & (-i))) fen[h][j][i] = max(fen[h][j][i], val);
}
int get(int h, int j, int i) {
  int res = 0;
  for (; i; i -= (i & (-i))) res = max(fen[h][j][i], res);
  return res;
}
void F() {
  for (int i = 1; i <= (int)(n); ++i)
    for (int j = 1; j <= (int)(m); ++j) r[i][j].clear();
  for (int i = 1; i <= (int)(Q); ++i) r[v[i].first][v[i].second].push_back(i);
  for (int i = 1; i <= (int)(n); ++i) {
    for (int j = 1; j <= (int)(m); ++j) {
      L[i][j] = U[i][j] = V[i][j] = 0;
      if (a[i][j]) {
        L[i][j] = L[i][j - 1] + 1;
        U[i][j] = U[i - 1][j] + 1;
        V[i][j] = min(V[i - 1][j - 1] + 1, min(L[i][j], U[i][j]));
      }
    }
  }
  int as = 1;
  for (int i = 1; i < N; ++i) {
    he[i] = he[i - 1];
    if ((as << 1) == i) {
      as <<= 1;
      ++he[i];
    }
  }
  for (int i = 1; i <= (int)(n); ++i) {
    for (int j = (int)(m); j >= 1; --j) {
      ma[0][i][j] = V[i][j];
      for (int h = 1; h <= (int)(H - 1); ++h) {
        ma[h][i][j] = ma[h - 1][i][j];
        int u = j + (1 << (h - 1));
        if (u <= m) ma[h][i][j] = max(ma[h][i][j], ma[h - 1][i][u]);
      }
    }
    for (int j = 1; j <= (int)(m); ++j) {
      for (int h = 0; h < (int)(H); ++h) update(h, j, n + 1 - i, ma[h][i][j]);
    }
    for (int j = 1; j <= (int)(m); ++j) {
      for (int e = 0; e < (int)(((int)((r[i][j]).size()))); ++e) {
        int in = r[i][j][e];
        int st = 1, fi = min(v[in].first - u[in].first + 1,
                             v[in].second - u[in].second + 1);
        while (st <= fi) {
          int md = (st + fi) >> 1;
          int sx = u[in].first + md - 1, fx = v[in].first,
              sy = u[in].second + md - 1, fy = v[in].second;
          int lo = he[fy - sy + 1];
          int val = max(get(lo, sy, n + 1 - sx),
                        get(lo, fy - (1 << lo) + 1, n + 1 - sx));
          if (val >= md) {
            st = md + 1;
            res[in] = md;
          } else
            fi = md - 1;
        }
      }
    }
  }
}
void solve() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= (int)(n); ++i)
    for (int j = 1; j <= (int)(m); ++j) scanf("%d", &a[i][j]);
  scanf("%d", &Q);
  for (int i = 1; i <= (int)(Q); ++i)
    scanf("%d%d%d%d", &u[i].first, &u[i].second, &v[i].first, &v[i].second);
  F();
  for (int i = 1; i <= (int)(Q); ++i) printf("%d\n", res[i]);
}
void testgen() {
  FILE* f = fopen("input.txt", "w");
  fclose(f);
}
int main() {
  std::cout << std::fixed;
  std::cout.precision(8);
  std::cerr << std::fixed;
  std::cerr.precision(3);
  solve();
  return 0;
}