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 <typename T>
using Matrix = vector<vector<T>>;
template <typename T>
Matrix<T> matrix(int R, int C, T x = T()) {
  return Matrix<T>(R, vector<T>(C, x));
}
struct SparseTable {
  vector<int> log;
  Matrix<Matrix<int>> data;
  SparseTable(const int R, const int C, const Matrix<int>& values) {
    log = vector<int>(max(R, C) + 1);
    for (auto i = 2; i <= max(R, C); i++) log[i] = log[i / 2] + 1;
    data =
        matrix<Matrix<int>>(log[R] + 1, log[C] + 1, matrix<int>(R + 1, C + 1));
    for (auto kr = 0; kr <= log[R]; kr++)
      for (auto kc = 0; kc <= log[C]; kc++)
        for (auto r = 1; r + (1 << kr) <= R + 1; r++)
          for (auto c = 1; c + (1 << kc) <= C + 1; c++) {
            auto& x = data[kr][kc][r][c];
            if (kr == 0 && kc == 0)
              x = values[r][c];
            else if (kr == 0)
              x = max(data[0][kc - 1][r][c],
                      data[0][kc - 1][r][c + (1 << (kc - 1))]);
            else if (kc == 0)
              x = max(data[kr - 1][0][r][c],
                      data[kr - 1][0][r + (1 << (kr - 1))][c]);
            else {
              const auto& d = data[kr - 1][kc - 1];
              const auto nr = r + (1 << (kr - 1));
              const auto nc = c + (1 << (kc - 1));
              x = max({d[r][c], d[r][nc], d[nr][c], d[nr][nc]});
            }
          }
  }
  int query(const int r1, const int c1, const int r2, const int c2) const {
    const auto kr = log[r2 - r1 + 1];
    const auto kc = log[c2 - c1 + 1];
    const auto& d = data[kr][kc];
    const auto nr1 = r2 - (1 << kr) + 1;
    const auto nc1 = c2 - (1 << kc) + 1;
    return max({d[r1][c1], d[r1][nc1], d[nr1][c1], d[nr1][nc1]});
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int R, C;
  cin >> R >> C;
  auto grid = matrix<int>(R + 1, C + 1);
  for (auto r = 1; r <= R; r++)
    for (auto c = 1; c <= C; c++) cin >> grid[r][c];
  auto max_square = matrix<int>(R + 1, C + 1);
  for (auto r = 1; r <= R; r++)
    for (auto c = 1; c <= C; c++) {
      if (grid[r][c]) {
        const auto constraints = {max_square[r - 1][c], max_square[r][c - 1],
                                  max_square[r - 1][c - 1]};
        max_square[r][c] = min(constraints) + 1;
      } else
        max_square[r][c] = 0;
    }
  const auto table = SparseTable(R, C, max_square);
  int t;
  cin >> t;
  for (auto i = 0; i < t; i++) {
    int r1, c1, r2, c2;
    cin >> r1 >> c1 >> r2 >> c2;
    auto lower_bound = 0, upper_bound = min(r2 - r1 + 1, c2 - c1 + 1);
    while (lower_bound < upper_bound) {
      const auto guess = (lower_bound + upper_bound + 1) / 2;
      if (table.query(r1 + guess - 1, c1 + guess - 1, r2, c2) >= guess)
        lower_bound = guess;
      else
        upper_bound = guess - 1;
    }
    cout << lower_bound << "\n";
  }
}