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 std::abs;
using std::bitset;
using std::cerr;
using std::cin;
using std::copy;
using std::cout;
using std::deque;
using std::endl;
using std::fill;
using std::fixed;
using std::greater;
using std::iota;
using std::istream;
using std::lower_bound;
using std::make_pair;
using std::make_tuple;
using std::map;
using std::max;
using std::max_element;
using std::min;
using std::min_element;
using std::multiset;
using std::next_permutation;
using std::nth_element;
using std::ostream;
using std::pair;
using std::priority_queue;
using std::queue;
using std::reverse;
using std::rotate;
using std::set;
using std::sort;
using std::string;
using std::swap;
using std::tie;
using std::tuple;
using std::unique;
using std::unordered_map;
using std::unordered_set;
using std::upper_bound;
using std::vector;
long long rnd(long long x, long long y) {
  static auto gen = std::bind(std::uniform_int_distribution<long long>(),
                              std::mt19937(960172));
  return gen() % (y - x + 1) + x;
}
long long gcd(long long a, long long b) {
  while (b > 0) {
    long long t = a % b;
    a = b;
    b = t;
  }
  return a;
}
template <typename T>
T sqr(T const& a) {
  return a * a;
}
int const INF = 100 + (int)1e9;
long long const INFL = 100 + (long long)1e18;
long double const PI = 3.1415926535897932384626433832795028;
int const N = 1003;
int const LG = 11;
int a[N][N];
short t[LG][LG][N][N];
int logs[N];
int get_max(int x1, int y1, int x2, int y2) {
  if (x2 <= x1 || y2 <= y1) {
    return 0;
  }
  int xx = logs[x2 - x1];
  int yy = logs[y2 - y1];
  return max(max(t[xx][yy][x1][y1], t[xx][yy][x1][y2 - (1 << yy)]),
             max(t[xx][yy][x2 - (1 << xx)][y1],
                 t[xx][yy][x2 - (1 << xx)][y2 - (1 << yy)]));
}
void solve() {
  logs[1] = 0;
  for (int i = 2; i < N; ++i) {
    logs[i] = 1 + logs[i / 2];
  }
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      scanf("%d", a[i] + j);
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (!a[i][j]) {
        t[0][0][i][j] = 0;
        continue;
      }
      if (i > 0 && j > 0) {
        int len = min(t[0][0][i - 1][j], t[0][0][i][j - 1]);
        if (a[i - len][j - len]) {
          ++len;
        }
        t[0][0][i][j] = len;
      } else {
        t[0][0][i][j] = 1;
      }
    }
  }
  for (int i = 0; 1 << i <= n; ++i) {
    int xlen = 1 << i;
    for (int j = 0; 1 << j <= m; ++j) {
      int ylen = 1 << j;
      for (int x = 0; x + xlen <= n; ++x) {
        for (int y = 0; y + ylen <= m; ++y) {
          if (i == 0) {
            if (j > 0) {
              t[i][j][x][y] =
                  max(t[i][j - 1][x][y], t[i][j - 1][x][y + ylen / 2]);
            }
          } else {
            t[i][j][x][y] =
                max(t[i - 1][j][x][y], t[i - 1][j][x + xlen / 2][y]);
          }
        }
      }
    }
  }
  int q;
  scanf("%d", &q);
  for (int it = 0; it < q; ++it) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    --x1, --y1;
    int l = 0;
    int r = n + 1;
    while (r - l > 1) {
      int m = (l + r) / 2;
      int cur = get_max(x1 + m - 1, y1 + m - 1, x2, y2);
      if (cur >= m) {
        l = m;
      } else {
        r = m;
      }
    }
    printf("%d\n", l);
  }
}
int main() {
  cout.precision(15);
  cout << fixed;
  cerr.precision(6);
  cerr << fixed;
  solve();
}