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 = 1005;
const int K = 22;
bool board[N][N];
short int rd[N][N];
int pow2[N];
int lg[N];
short int table[N][N][K / 2][K / 2];
int n, m;
void precalc() {
  for (int i = 0; i < K; i++) pow2[i] = (i == 0 ? 1 : pow2[i - 1] * 2);
  for (int i = 1; i < N; i++)
    for (int j = 0; j < K; j++)
      if (i >= pow2[j]) lg[i] = j;
}
void build_table() {
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      for (int h = 0; i + pow2[h] <= n; h++) {
        for (int k = 0; j + pow2[k] <= m; k++) {
          if (h == 0 && k == 0) {
            table[i][j][0][0] = rd[i][j];
            continue;
          }
          if (h > 0)
            table[i][j][h][k] =
                max(table[i][j][h - 1][k], table[i + pow2[h - 1]][j][h - 1][k]);
          else
            table[i][j][h][k] =
                max(table[i][j][h][k - 1], table[i][j + pow2[k - 1]][h][k - 1]);
        }
      }
    }
  }
}
short int get_max(int x1, int y1, int x2, int y2) {
  int h = lg[x2 - x1];
  int k = lg[y2 - y1];
  short int ans = table[x1][y1][h][k];
  ans = max(ans, table[x2 - pow2[h]][y1][h][k]);
  ans = max(ans, table[x1][y2 - pow2[k]][h][k]);
  ans = max(ans, table[x2 - pow2[h]][y2 - pow2[k]][h][k]);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  precalc();
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> board[i][j];
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      if (!board[i][j]) {
        rd[i][j] = 0;
        continue;
      }
      if (i == n - 1 || j == m - 1)
        rd[i][j] = 1;
      else
        rd[i][j] = 1 + min(rd[i + 1][j], min(rd[i][j + 1], rd[i + 1][j + 1]));
    }
  }
  build_table();
  int t;
  cin >> t;
  for (; t > 0; t--) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1--;
    y1--;
    int l = 0;
    int r = pow2[K / 2];
    while (r - l > 1) {
      int tm = (l + r) / 2;
      if (x2 - x1 < tm || y2 - y1 < tm) {
        r = tm;
        continue;
      }
      if (get_max(x1, y1, x2 - tm + 1, y2 - tm + 1) < tm)
        r = tm;
      else
        l = tm;
    }
    cout << l << "\n";
  }
  return 0;
}