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 = 1005;
int f[N][N];
int s[N][N];
const int LOG = 10;
int sp[LOG][N][N];
int lg[N];
const int LEVEL = (1 << LOG);
const int SZ = (LEVEL << 1);
struct Tree {
  int mx[SZ];
  Tree() : mx() {}
  void build(int a[N]) {
    for (int i = 0; i < N; i++) mx[LEVEL + i] = a[i];
    for (int i = LEVEL - 1; i >= 1; i--) mx[i] = max(mx[2 * i], mx[2 * i + 1]);
  }
  int getMax(int v, int l, int r, int tl, int tr) {
    if (tr < l || tl > r) return 0;
    if (l <= tl && tr <= r) return mx[v];
    int tm = (tl + tr) / 2;
    return max(getMax(2 * v, l, r, tl, tm),
               getMax(2 * v + 1, l, r, tm + 1, tr));
  }
  int getMax(int l, int r) { return getMax(1, l, r, 0, LEVEL - 1); }
} tree[LOG][N];
int getMax(int x1, int y1, int x2, int y2) {
  int l = lg[x2 - x1 + 1];
  return max(tree[l][x1].getMax(y1, y2),
             tree[l][x2 - (1 << l) + 1].getMax(y1, y2));
}
int main(int, char **) {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", &f[i][j]);
  for (int i = n - 1; i >= 0; i--)
    for (int j = m - 1; j >= 0; j--) {
      if (f[i][j] == 0) continue;
      s[i][j] = min({s[i + 1][j], s[i][j + 1], s[i + 1][j + 1]}) + 1;
    }
  lg[1] = 0;
  for (int i = 2; i <= max(n, m); i++) {
    lg[i] = lg[i - 1];
    if ((1 << (lg[i] + 1)) <= i) lg[i]++;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) sp[0][i][j] = s[i][j];
  for (int l = 1; l < LOG; l++)
    for (int i = 0; i + (1 << l) <= n; i++)
      for (int j = 0; j < m; j++)
        sp[l][i][j] = max(sp[l - 1][i][j], sp[l - 1][i + (1 << (l - 1))][j]);
  for (int l = 0; l < LOG; l++)
    for (int i = 0; i < n; i++) tree[l][i].build(sp[l][i]);
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    x1--;
    y1--;
    x2--;
    y2--;
    int lb = 0, rb = min(y2 - y1 + 1, x2 - x1 + 1) + 1;
    while (rb - lb > 1) {
      int mb = (lb + rb) / 2;
      int x = getMax(x1, y1, x2 - mb + 1, y2 - mb + 1);
      if (x >= mb)
        lb = mb;
      else
        rb = mb;
    }
    printf("%d\n", lb);
  }
  return 0;
}