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 N = 1010;
const long long INF = 1e9 + 19;
const int LOG = 11;
int n, m;
int a[N][N];
int b[N][N];
int pref[N][N];
short table[LOG][LOG][N][N];
int deg[N];
void read() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
}
int getSum(int x1, int y1, int x2, int y2) {
  return pref[x2][y2] - pref[x1][y2] - pref[x2][y1] + pref[x1][y1];
}
bool isSquare(int x1, int y1, int x2, int y2) {
  return (x2 - x1) * (y2 - y1) == getSum(x1, y1, x2, y2);
}
int getMax(int x1, int y1, int x2, int y2) {
  int szX = x2 - x1;
  int szY = y2 - y1;
  int dx = deg[szX];
  int dy = deg[szY];
  int tmp = max(max(table[dx][dy][x1][y1], table[dx][dy][x2 - (1 << dx)][y1]),
                max(table[dx][dy][x1][y2 - (1 << dy)],
                    table[dx][dy][x2 - (1 << dx)][y2 - (1 << dy)]));
  return tmp;
}
void solve() {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      pref[i + 1][j + 1] =
          pref[i + 1][j] + pref[i][j + 1] - pref[i][j] + a[i][j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      int l = 0;
      int r = min(n - i, m - j) + 1;
      while (r - l > 1) {
        int mid = (l + r) / 2;
        if (isSquare(i, j, i + mid, j + mid))
          l = mid;
        else
          r = mid;
      }
      b[i][j] = l;
    }
  for (int i = 2; i <= max(n, m); i++) deg[i] = deg[i / 2] + 1;
  for (int dx = 0; dx <= 10; dx++)
    for (int dy = 0; dy <= 10; dy++)
      for (int i = 0; i + (1 << dx) <= n; i++)
        for (int j = 0; j + (1 << dy) <= m; j++) {
          if (dx == 0 && dy == 0) {
            table[0][0][i][j] = b[i][j];
          } else if (dx != 0) {
            table[dx][dy][i][j] =
                max(table[dx - 1][dy][i][j],
                    table[dx - 1][dy][i + (1 << (dx - 1))][j]);
          } else
            table[dx][dy][i][j] =
                max(table[dx][dy - 1][i][j],
                    table[dx][dy - 1][i][j + (1 << (dy - 1))]);
        }
  int t;
  scanf("%d", &t);
  for (int it = 0; it < t; it++) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    x1--;
    y1--;
    int l = 0;
    int r = min(x2 - x1, y2 - y1) + 1;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (getMax(x1, y1, x2 - mid + 1, y2 - mid + 1) >= mid)
        l = mid;
      else
        r = mid;
    }
    printf("%d\n", l);
  }
}
void stress() {}
int main() {
  if (1) {
    int k = 1;
    for (int tt = 0; tt < k; tt++) {
      read();
      solve();
    }
  } else {
    stress();
  }
  return 0;
}