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;
template <class T>
struct rmq_2d {
  vector<vector<vector<vector<T>>>> t;
  rmq_2d() {}
  void init(const int n, const int m) {
    const int logn = 32 - __builtin_clz(n);
    const int logm = 32 - __builtin_clz(m);
    t.resize(logn, vector<vector<vector<T>>>(
                       logm, vector<vector<T>>(n, vector<T>(m))));
  }
  rmq_2d(int n, int m) { init(n, m); }
  vector<T>& operator[](int i) { return t[0][0][i]; }
  inline T combine(T a, T b) { return max(a, b); }
  void build() {
    const int logn = t.size();
    const int logm = t[0].size();
    const int n = t[0][0].size();
    const int m = t[0][0][0].size();
    for (int i = 0; i < logn; ++i) {
      for (int j = 0; j < logm; ++j) {
        if (i == 0 && j == 0) continue;
        for (int k = 0; k < n; ++k) {
          for (int l = 0; l < m; ++l) {
            if (j == 0)
              t[i][j][k][l] =
                  combine(t[i - 1][j][k][l],
                          t[i - 1][j][min(n - 1, k + (1 << (i - 1)))][l]);
            else
              t[i][j][k][l] =
                  combine(t[i][j - 1][k][l],
                          t[i][j - 1][k][min(m - 1, l + (1 << (j - 1)))]);
          }
        }
      }
    }
  }
  T query(int a, int b, int c, int d) {
    const int i = 31 - __builtin_clz(c - a + 1);
    const int j = 31 - __builtin_clz(d - b + 1);
    const int dx = (1 << i) - 1;
    const int dy = (1 << j) - 1;
    T ans = t[i][j][a][b];
    ans = combine(ans, t[i][j][c - dx][b]);
    ans = combine(ans, t[i][j][a][d - dy]);
    ans = combine(ans, t[i][j][c - dx][d - dy]);
    return ans;
  }
};
const int N = 1005;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n, m, q;
  cin >> n >> m;
  rmq_2d<int> R(n, m);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> R[i][j];
      if (R[i][j] && i > 0 && j > 0)
        R[i][j] = 1 + min(R[i - 1][j], min(R[i][j - 1], R[i - 1][j - 1]));
    }
  }
  cin >> q;
  R.build();
  for (int i = 0; i < q; ++i) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    --a, --b, --c, --d;
    int lo = 0, hi = min(d - b + 1, c - a + 1) + 1;
    while (hi - lo > 1) {
      int md = (lo + hi) / 2;
      if (R.query(a + md - 1, b + md - 1, c, d) >= md)
        lo = md;
      else
        hi = md;
    }
    cout << lo << '\n';
  }
}