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;
short answers[1000][1000][11][11];
void Preprocess(int n, int m, const vector<vector<int> >& field) {
  vector<vector<int> > max_right(n, vector<int>(m));
  vector<vector<int> > max_square(n, vector<int>(m));
  for (int i = 0; i < n; ++i) {
    if (field[i][m - 1] == 1) {
      max_right[i][m - 1] = max_square[i][m - 1] = 1;
    } else {
      max_right[i][m - 1] = max_square[i][m - 1] = 0;
    }
  }
  for (int col = m - 2; col >= 0; --col) {
    for (int row = n - 1; row >= 0; --row) {
      if (field[row][col] == 0) {
        max_right[row][col] = max_square[row][col] = 0;
      } else {
        if (field[row][col + 1] == 1) {
          max_right[row][col] = max_right[row][col + 1] + 1;
        } else {
          max_right[row][col] = 1;
        }
        if (row == n - 1 || max_square[row + 1][col] == 0) {
          max_square[row][col] = 1;
        } else {
          if (max_right[row][col] <= max_square[row + 1][col]) {
            max_square[row][col] = max_right[row][col];
          } else {
            if (max_square[row + 1][col + 1] >= max_square[row + 1][col]) {
              max_square[row][col] = max_square[row + 1][col] + 1;
            } else {
              max_square[row][col] = max_square[row + 1][col];
            }
          }
        }
      }
    }
  }
  for (int row = n - 1; row >= 0; --row) {
    for (int col = m - 1; col >= 0; --col) {
      answers[row][col][0][0] = max_square[row][col];
      for (int level1 = 0; level1 <= 10; ++level1) {
        for (int level2 = 0; level2 <= 10; ++level2) {
          if (level2 > 0) {
            answers[row][col][level1][level2] =
                answers[row][col][level1][level2 - 1];
            int shift = (1 << (level2 - 1));
            if (col + shift < m) {
              answers[row][col][level1][level2] =
                  max(answers[row][col][level1][level2],
                      answers[row][col + shift][level1][level2 - 1]);
            }
          } else if (level1 > 0) {
            answers[row][col][level1][level2] =
                answers[row][col][level1 - 1][level2];
            int shift = (1 << (level1 - 1));
            if (row + shift < n) {
              answers[row][col][level1][level2] =
                  max(answers[row][col][level1][level2],
                      answers[row + shift][col][level1 - 1][level2]);
            }
          }
        }
      }
    }
  }
}
int GetMaxSquareInside(int row1, int col1, int row2, int col2) {
  int low = 0;
  int high = min(row2 - row1 + 1, col2 - col1 + 1);
  while (low < high) {
    int mid = (low + high + 1) / 2;
    int nrow1 = row1;
    int ncol1 = col1;
    int nrow2 = row2 - mid + 1;
    int ncol2 = col2 - mid + 1;
    int width = 1;
    int level2 = 0;
    while (width * 2 <= ncol2 - ncol1 + 1) {
      width *= 2;
      ++level2;
    }
    int height = 1;
    int level1 = 0;
    while (height * 2 <= nrow2 - nrow1 + 1) {
      height *= 2;
      ++level1;
    }
    int res = max(
        max(answers[nrow1][ncol1][level1][level2],
            answers[nrow2 - height + 1][ncol1][level1][level2]),
        max(answers[nrow1][ncol2 - width + 1][level1][level2],
            answers[nrow2 - height + 1][ncol2 - width + 1][level1][level2]));
    if (res >= mid) {
      low = mid;
    } else {
      high = mid - 1;
    }
  }
  return low;
}
bool IsFilled(const vector<vector<int> >& field, int row1, int col1, int row2,
              int col2) {
  for (int row = row1; row <= row2; ++row) {
    for (int col = col1; col <= col2; ++col) {
      if (!field[row][col]) {
        return false;
      }
    }
  }
  return true;
}
int GetMaxSquareInsideStupid(int n, int m, const vector<vector<int> >& field,
                             int row1, int col1, int row2, int col2) {
  int res = 0;
  for (int row = row1; row <= row2; ++row) {
    for (int col = col1; col <= col2; ++col) {
      for (int size = 1; size <= min(row2 - row + 1, col2 - col + 1); ++size) {
        if (IsFilled(field, row, col, row + size - 1, col + size - 1)) {
          res = max(res, size);
        }
      }
    }
  }
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > field(n, vector<int>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      scanf("%d", &field[i][j]);
    }
  }
  Preprocess(n, m, field);
  int queries;
  scanf("%d", &queries);
  for (int qi = 0; qi < queries; ++qi) {
    int row1, col1, row2, col2;
    scanf("%d%d%d%d", &row1, &col1, &row2, &col2);
    --row1;
    --col1;
    --row2;
    --col2;
    printf("%d\n", GetMaxSquareInside(row1, col1, row2, col2));
  }
  return 0;
}