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 INF = 1e9;
const int N = 1031;
const int T = 1000031;
int n, m, ar[N][N];
short SZ[N][N];
int s[N][N];
bool bad_cell(int a, int b, int side) {
  if (a + side - 1 > n || b + side - 1 > m) return true;
  int area = side * side;
  int here = s[a + side - 1][b + side - 1] - s[a + side - 1][b - 1] -
             s[a - 1][b + side - 1] + s[a - 1][b - 1];
  return (here != area);
}
int gbest;
int tests;
int x1[T], aasdfasdfasdf[T], x2[T], y2[T];
int l[T], r[T];
vector<pair<int, int> > events[N];
int t[N][N];
vector<int> checklist[N];
inline int sum(int x, int y) {
  int res = 0;
  for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
    for (int j = y; j >= 0; j = (j & (j + 1)) - 1) res += t[i][j];
  }
  return res;
}
inline void inc(int x, int y, int val) {
  for (int i = x; i <= n; i = (i | (i + 1))) {
    for (int j = y; j <= m; j = (j | (j + 1))) {
      t[i][j] += val;
    }
  }
}
short AR[N][N];
int S[50][N][N];
const int STEP = 32;
int next_divisible(int val) {
  if (val % STEP == 0) return val + STEP;
  return val - val % STEP + STEP;
}
bool check(int id, int whr) {
  if (whr == 0) return true;
  return S[whr][x2[id] - whr * STEP + 1][y2[id] - whr * STEP + 1] -
         S[whr][x1[id] - 1][y2[id] - whr * STEP + 1] -
         S[whr][x2[id] - whr * STEP + 1][aasdfasdfasdf[id] - 1] +
         S[whr][x1[id] - 1][aasdfasdfasdf[id] - 1];
}
int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &ar[i][j]);
      s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + ar[i][j];
    }
  }
  for (int i = n; i; i--) {
    for (int j = m; j; --j) {
      SZ[i][j] = SZ[i + 1][j + 1] + 1;
      while (bad_cell(i, j, SZ[i][j])) --SZ[i][j];
      gbest = max(gbest, (int)SZ[i][j]);
      if (SZ[i][j]) events[SZ[i][j]].push_back(make_pair(i, j));
    }
  }
  for (int iter = max(n, m); iter >= 0; iter--) {
    for (int i = 0; i < events[iter].size(); i++) {
      pair<int, int> p = events[iter][i];
      AR[p.first][p.second] = 1;
    }
    if (iter % STEP == 0) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
          S[iter / STEP][i][j] = S[iter / STEP][i - 1][j] +
                                 S[iter / STEP][i][j - 1] -
                                 S[iter / STEP][i - 1][j - 1] + AR[i][j];
        }
      }
    }
  }
  scanf("%d", &tests);
  for (int test = 1; test <= tests; test++) {
    scanf("%d", &x1[test]);
    scanf("%d", &aasdfasdfasdf[test]);
    scanf("%d", &x2[test]);
    scanf("%d", &y2[test]);
    l[test] = 0;
    r[test] = min(x2[test] - x1[test] + 1, y2[test] - aasdfasdfasdf[test] + 1);
    r[test] = min(r[test], gbest);
    while (r[test] - l[test] >= STEP) {
      int v = next_divisible(l[test]);
      if (check(test, v / STEP))
        l[test] = v;
      else
        r[test] = v - 1;
    }
  }
  while (true) {
    for (int i = 0; i <= 1000; i++) {
      checklist[i].clear();
    }
    int flag = 0;
    for (int i = 1; i <= tests; i++) {
      if (l[i] == r[i]) continue;
      flag = 1;
      int mid = l[i] + r[i] + 1;
      mid /= 2;
      checklist[mid].push_back(i);
    }
    if (flag == 0) break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        t[i][j] = 0;
      }
    }
    for (int i = 1000; i >= 0; i--) {
      for (int j = 0; j < events[i].size(); j++) {
        int tx = events[i][j].first;
        int ty = events[i][j].second;
        inc(tx, ty, 1);
      }
      for (int j = 0; j < checklist[i].size(); j++) {
        int id = checklist[i][j];
        if (i == 0) {
          l[id] = 0;
          continue;
        }
        int here = sum(x2[id] - i + 1, y2[id] - i + 1) -
                   sum(x1[id] - 1, y2[id] - i + 1) -
                   sum(x2[id] - i + 1, aasdfasdfasdf[id] - 1) +
                   sum(x1[id] - 1, aasdfasdfasdf[id] - 1);
        if (here)
          l[id] = i;
        else
          r[id] = i - 1;
      }
    }
  }
  for (int i = 1; i <= tests; i++) {
    printf("%d\n", l[i]);
  }
  cin.get();
  cin.get();
  return 0;
}