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;
vector<int> logr;
vector<vector<vector<vector<int>>>> sparse;
int x4, y4, x2, y2;
inline int check(int m) {
  int x3 = x4 + m - 1;
  int y3 = y4 + m - 1;
  if (x3 > x2 || y3 > y2) return false;
  int i = logr[x2 - x3];
  int j = logr[y2 - y3];
  int res = max(max(sparse[i][j][x3][y3], sparse[i][j][x2 - (1 << i) + 1][y3]),
                max(sparse[i][j][x3][y2 - (1 << j) + 1],
                    sparse[i][j][x2 - (1 << i) + 1][y2 - (1 << j) + 1]));
  return res >= m;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  logr.resize(max(n, m) + 1, 0);
  for (int i = 2; i < int(logr.size()); i++) {
    logr[i] = logr[i / 2] + 1;
  }
  vector<vector<int>> b(n + 1, vector<int>(m + 1));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> b[i][j];
    }
  }
  vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (b[i][j] == 0) continue;
      dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
    }
  }
  sparse.resize(logr[n] + 1,
                vector<vector<vector<int>>>(
                    logr[m] + 1, vector<vector<int>>(n, vector<int>(m, -1e9))));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      sparse[0][0][i][j] = dp[i + 1][j + 1];
    }
  }
  for (int i = 0; i <= logr[n]; i++) {
    for (int j = 0; j <= logr[m]; j++) {
      if (i == 0 && j == 0) continue;
      for (int x = 0; x <= n - (1 << i); x++) {
        for (int y = 0; y <= m - (1 << j); y++) {
          int e = max(i - 1, 0);
          int g = max(j - 1, 0);
          int t = (i == 0 ? 0 : (1 << e));
          int r = (j == 0 ? 0 : (1 << g));
          sparse[i][j][x][y] =
              max(max(sparse[e][g][x][y], sparse[e][g][x + t][y]),
                  max(sparse[e][g][x][y + r], sparse[e][g][x + t][y + r]));
        }
      }
    }
  }
  int t;
  cin >> t;
  while (t--) {
    cin >> x4 >> y4 >> x2 >> y2;
    x4--;
    y4--;
    x2--;
    y2--;
    int L = 0;
    int R = min(x2 - x4, y2 - y4) + 2;
    while (R - L > 1) {
      int m = (R + L) / 2;
      if (check(m))
        L = m;
      else
        R = m;
    }
    cout << L << '\n';
  }
  return 0;
}