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;
const int MAXN = 1100;
const int MAXB = 10;
int s[MAXN][MAXN];
short p[MAXB][MAXB][MAXN][MAXN];
int lg[MAXN];
inline int read() {
  int x = 0;
  char ch = 0;
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x;
}
inline int query(int l, int d, int r, int u) {
  int x = lg[r - l + 1], y = lg[u - d + 1];
  return max(max(p[x][y][l][d], p[x][y][r - (1 << x) + 1][u - (1 << y) + 1]),
             max(p[x][y][r - (1 << x) + 1][d], p[x][y][l][u - (1 << y) + 1]));
}
int main() {
  int n, m, q;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      s[i][j] = s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1] + (read() ^ 1);
      int l = 0, r = min(i, j);
      while (l < r) {
        int mid = l + r + 1 >> 1;
        if (s[i][j] - s[i - mid][j] - s[i][j - mid] + s[i - mid][j - mid])
          r = mid - 1;
        else
          l = mid;
      }
      p[0][0][i][j] = l;
    }
  for (int i = 1, l = 0; i <= n || i <= m; i++) lg[i] = l += 1 << l + 1 <= i;
  for (int k = 1; 1 << k <= n; k++)
    for (int i = 1; i + (1 << k) - 1 <= n; i++)
      for (int j = 1; j <= m; j++)
        p[k][0][i][j] =
            max(p[k - 1][0][i][j], p[k - 1][0][i + (1 << k - 1)][j]);
  for (int x = 0; 1 << x <= n; x++)
    for (int y = 1; 1 << y <= m; y++)
      for (int i = 1; i + (1 << x) - 1 <= n; i++)
        for (int j = 1; j + (1 << y) - 1 <= m; j++)
          p[x][y][i][j] =
              max(p[x][y - 1][i][j], p[x][y - 1][i][j + (1 << y - 1)]);
  scanf("%d", &q);
  while (q--) {
    int x1 = read(), y1 = read(), x2 = read(), y2 = read();
    int l = 0, r = min(x2 - x1 + 1, y2 - y1 + 1);
    while (l < r) {
      int mid = l + r + 1 >> 1;
      if (query(x1 + mid - 1, y1 + mid - 1, x2, y2) >= mid)
        l = mid;
      else
        r = mid - 1;
    }
    printf("%d\n", l);
  }
  return 0;
}