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;
int mat[1005][1005];
int sm[1005][1005];
int sq[1005][1005];
short sparse[10][10][1005][1005];
int lg[1005];
void pre(int n, int m) {
  for (int g = 0; g < 10; g++) {
    int lim = min((1 << (g + 1)), 1005);
    for (int i = 1 << g; i < lim; i++) lg[i] = g;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) sparse[0][0][i][j] = sq[i][j];
    for (int g = 1; (1 << g) <= m; g++) {
      int p2 = 1 << g;
      for (int j = 1; j + p2 - 1 <= m; j++) {
        sparse[g][0][i][j] =
            max(sparse[g - 1][0][i][j], sparse[g - 1][0][i][j + (p2 >> 1)]);
      }
    }
  }
  for (int j = 1; j <= m; j++) {
    for (int g = 0; g <= lg[m - j + 1]; g++) {
      for (int g2 = 1; g2 <= lg[n]; g2++) {
        int p2 = (1 << g2);
        for (int i = 1; i + p2 - 1 <= n; i++) {
          sparse[g][g2][i][j] =
              max(sparse[g][g2 - 1][i][j], sparse[g][g2 - 1][i + (p2 >> 1)][j]);
        }
      }
    }
  }
}
int max2D(int x1, int y1, int x2, int y2) {
  int g2 = lg[x2 - x1 + 1], g = lg[y2 - y1 + 1];
  int f1 = max(sparse[g][g2][x1][y1], sparse[g][g2][x2 - (1 << g2) + 1][y1]);
  int f2 = max(sparse[g][g2][x1][y2 - (1 << g) + 1],
               sparse[g][g2][x2 - (1 << g2) + 1][y2 - (1 << g) + 1]);
  return max(f1, f2);
}
int main() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  memset(sm, 0, sizeof(sm));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &mat[i][j]);
      sm[i][j] = mat[i][j] + sm[i][j - 1] + sm[i - 1][j] - sm[i - 1][j - 1];
    }
  }
  memset(sq, 0, sizeof(sq));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!mat[i][j]) continue;
      int l = 1, r = min(i, j);
      while (l < r) {
        int mid = (l + r + 1) >> 1;
        int s =
            sm[i][j] - sm[i - mid][j] - sm[i][j - mid] + sm[i - mid][j - mid];
        if (s == mid * mid)
          l = mid;
        else
          r = mid - 1;
      }
      sq[i][j] = l;
    }
  }
  pre(n, m);
  int t;
  scanf("%d", &t);
  while (t--) {
    int x1, y1, x2, y2;
    scanf("%d", &x1);
    scanf("%d", &y1);
    scanf("%d", &x2);
    scanf("%d", &y2);
    int l = 0, r = min(x2 - x1 + 1, y2 - y1 + 1);
    while (l < r) {
      int mid = (l + r + 1) >> 1;
      int q = max2D(x1 + mid - 1, y1 + mid - 1, x2, y2);
      if (q >= mid)
        l = mid;
      else
        r = mid - 1;
    }
    printf("%d\n", l);
  }
}