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 long double EPS = 1e-9;
const long double PI = acos(-1.0);
const int INF = (int)1e9 + 5;
const long long LINF = (long long)1e18 + 5;
bool eq(double a, double b) { return fabs(a - b) < EPS; }
template <class T>
T sqr(const T& x) {
  return x * x;
}
template <class T>
long long abs(const T& x) {
  return x < 0 ? -x : x;
}
template <class T>
long long myround(const T& x) {
  return x < 0 ? x - 0.5 : x + 0.5;
}
template <class T>
bool chmin(T& x, const T& y) {
  if (y < x) {
    x = y;
    return true;
  }
  return false;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
    os << *it;
    if ((it + 1) != v.end()) os << ", ";
  }
  os << "}";
  return os;
}
template <class P, class Q>
ostream& operator<<(ostream& os, const pair<P, Q>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
bool chmax(T& x, const T& y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
int nextInt() {
  int x = 0;
  bool p = false;
  char c;
  do {
    c = getchar();
  } while (c <= 32);
  if (c == '-') {
    p = true;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return (p ? -x : x);
}
const int N = 1010;
const int Q = (int)1e6 + 5;
int n, m;
int field[N][N];
vector<pair<int, int>> pts[N];
int ql[Q], qr[Q], qmid[Q], q, x[Q], y[Q], xx[Q], yy[Q];
int rightDP[N][N], downDP[N][N], F[N][N];
bool ok() {
  bool res = false;
  for (int i = 1; i <= q; i++) {
    qmid[i] = (ql[i] + qr[i]) / 2;
    if (qr[i] - ql[i] > 1) res = true;
  }
  return res;
}
int ft[N][N];
void addFT(int x, int y, int val) {
  int rx = x;
  while (rx < N) {
    int ry = y;
    while (ry < N) {
      ft[rx][ry] += val;
      ry |= ry + 1;
    }
    rx |= rx + 1;
  }
}
int sumFT(int x, int y) {
  int res = 0;
  int rx = x;
  while (rx >= 0) {
    int ry = y;
    while (ry >= 0) {
      res += ft[rx][ry];
      ry = (ry & (ry + 1)) - 1;
    }
    rx = (rx & (rx + 1)) - 1;
  }
  return res;
}
int sum(int x, int y, int xx, int yy) {
  if (x > xx || y > yy) return 0;
  return sumFT(xx, yy) - sumFT(xx, y - 1) - sumFT(x - 1, yy) +
         sumFT(x - 1, y - 1);
}
void process() {
  static vector<int> qrIdxs[N];
  for (int i = 0; i <= n + 1; i++) {
    qrIdxs[i].resize(0);
  }
  for (int i = 1; i <= q; i++) {
    qrIdxs[qmid[i]].push_back(i);
  }
  memset(ft, 0, sizeof(ft));
  for (int i = n; i >= 1; i--) {
    for (const auto& p : pts[i]) {
      addFT(p.first, p.second, 1);
    }
    for (int idx : qrIdxs[i]) {
      int s = qmid[idx];
      if (sum(x[idx], y[idx], xx[idx] - s + 1, yy[idx] - s + 1))
        ql[idx] = qmid[idx];
      else
        qr[idx] = qmid[idx];
    }
  }
}
int main() {
  srand(time(0));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      field[i][j] = nextInt();
    }
  }
  for (int i = n; i >= 1; i--) {
    for (int j = m; j >= 1; j--) {
      if (field[i][j] == 0) {
        rightDP[i][j] = downDP[i][j] = F[i][j] = 0;
      } else {
        F[i][j] =
            min(rightDP[i][j + 1], min(downDP[i + 1][j], F[i + 1][j + 1])) + 1;
        rightDP[i][j] = rightDP[i][j + 1] + 1;
        downDP[i][j] = downDP[i + 1][j] + 1;
      }
      pts[F[i][j]].push_back({i, j});
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
    }
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    x[i] = nextInt();
    y[i] = nextInt();
    xx[i] = nextInt();
    yy[i] = nextInt();
    ql[i] = 0, qr[i] = min(xx[i] - x[i] + 1, yy[i] - y[i] + 1) + 1;
  }
  while (ok()) {
    process();
  }
  for (int i = 1; i <= q; i++) {
    printf("%d\n", ql[i]);
  }
}