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 getint() {
  long long _x = 0, _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
  if (_tc == '-') _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
  return _x * _tmp;
}
inline long long add(long long _x, long long _y,
                     long long _mod = 1000000007ll) {
  long long _ = _x + _y;
  if (_ >= _mod) _ -= _mod;
  return _;
}
inline long long sub(long long _x, long long _y,
                     long long _mod = 1000000007ll) {
  long long _ = _x - _y;
  if (_ < 0) _ += _mod;
  return _;
}
inline long long mul(long long _x, long long _y,
                     long long _mod = 1000000007ll) {
  long long _ = _x * _y;
  if (_ >= _mod) _ %= _mod;
  return _;
}
long long mypow(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 1ll;
  long long _tmp = mypow(_a, _x / 2, _mod);
  _tmp = mul(_tmp, _tmp, _mod);
  if (_x & 1) _tmp = mul(_tmp, _a, _mod);
  return _tmp;
}
long long mymul(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 0ll;
  long long _tmp = mymul(_a, _x / 2, _mod);
  _tmp = add(_tmp, _tmp, _mod);
  if (_x & 1) _tmp = add(_tmp, _a, _mod);
  return _tmp;
}
inline bool equal(double _x, double _y) {
  return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, _cs;
short n, m, a[1021][1021], two[10], bt[1021];
short dp[1021][1021][10][10];
void build_rmq() {
  for (short ib = 0; ib < 10; ib++)
    for (short jb = 0; jb < 10; jb++) {
      if (ib == 0 && jb == 0) continue;
      for (short i = 1; i + two[ib] - 1 <= n; i++)
        for (short j = 1; j + two[jb] - 1 <= m; j++) {
          if (jb)
            dp[i][j][ib][jb] =
                max(dp[i][j][ib][jb - 1], dp[i][j + two[jb - 1]][ib][jb - 1]);
          if (ib)
            dp[i][j][ib][jb] =
                max(dp[i][j][ib - 1][jb], dp[i + two[ib - 1]][j][ib - 1][jb]);
        }
    }
}
int query(int ql, int qr, int qu, int qd) {
  int ibt = bt[qr - ql + 1];
  int jbt = bt[qd - qu + 1];
  return max(max(dp[ql][qu][ibt][jbt], dp[ql][qd - two[jbt] + 1][ibt][jbt]),
             max(dp[qr - two[ibt] + 1][qu][ibt][jbt],
                 dp[qr - two[ibt] + 1][qd - two[jbt] + 1][ibt][jbt]));
}
void build() {
  two[0] = 1;
  for (int i = 1; i < 10; i++) two[i] = two[i - 1] * 2;
  short tbt = 0, nxt = 2;
  for (int i = 1; i < 1021; i++) {
    bt[i] = tbt;
    if (i == nxt) {
      nxt <<= 1;
      tbt++;
    }
  }
}
void init() {
  n = getint();
  m = getint();
  for (short i = 1; i <= n; i++)
    for (short j = 1; j <= m; j++) a[i][j] = getint();
  for (short i = 1; i <= n; i++)
    for (short j = 1; j <= m; j++)
      if (a[i][j])
        dp[i][j][0][0] = 1 + min(dp[i - 1][j - 1][0][0],
                                 min(dp[i - 1][j][0][0], dp[i][j - 1][0][0]));
  build_rmq();
}
void solve() {
  int qry = getint();
  while (qry--) {
    short al = getint();
    short au = getint();
    short ar = getint();
    short ad = getint();
    short bl = 0, br = min(ar - al + 1, ad - au + 1), ans = 0;
    short ql, qr, qu, qd;
    qr = ar;
    qd = ad;
    while (bl <= br) {
      short bmid = (bl + br) >> 1;
      ql = al + bmid - 1;
      qu = au + bmid - 1;
      if (query(ql, qr, qu, qd) >= bmid)
        ans = bmid, bl = bmid + 1;
      else
        br = bmid - 1;
    }
    printf("%d\n", (int)ans);
  }
}
int main() {
  build();
  while (__--) {
    init();
    solve();
  }
}