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;
int n, m, a[1004][1004], d[1004][1004];
short st[1005][1005][11][11];
int pw[20], lg[1005];
int x, y, xx, yy;
inline bool ck(int len) {
  int sx = x + len - 1, sy = y + len - 1;
  return max(max(st[sx][sy][lg[xx - sx + 1]][lg[yy - sy + 1]],
                 st[xx - pw[lg[xx - sx + 1]] + 1][yy - pw[lg[yy - sy + 1]] + 1]
                   [lg[xx - sx + 1]][lg[yy - sy + 1]]),
             max(st[xx - pw[lg[xx - sx + 1]] + 1][sy][lg[xx - sx + 1]]
                   [lg[yy - sy + 1]],
                 st[sx][yy - pw[lg[yy - sy + 1]] + 1][lg[xx - sx + 1]]
                   [lg[yy - sy + 1]])) >= len;
}
int main() {
  pw[0] = 1, lg[1] = 0;
  for (int i = 1; i <= 20; i++) pw[i] = (pw[i - 1] << 1);
  for (int i = 2; i <= 1001; i++) lg[i] = lg[i >> 1] + 1;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
      st[i][j][0][0] = d[i][j] =
          a[i][j] * (min(min(d[i][j - 1], d[i - 1][j]), d[i - 1][j - 1]) + 1);
    }
  for (int k1 = 0; k1 <= 9; k1++)
    for (int k2 = 0; k2 <= 9; k2++)
      if (k1 | k2)
        for (int i = 1; i <= n; i++)
          for (int j = 1; j <= m; j++)
            if (i + pw[k1] - 1 <= n && j + pw[k2] - 1 <= m) {
              if (k2 != 0)
                st[i][j][k1][k2] = max(st[i][j][k1][k2 - 1],
                                       st[i][j + pw[k2 - 1]][k1][k2 - 1]);
              else
                st[i][j][k1][k2] = max(st[i][j][k1 - 1][k2],
                                       st[i + pw[k1 - 1]][j][k1 - 1][k2]);
            } else
              break;
  int Q;
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    scanf("%d%d%d%d", &x, &y, &xx, &yy);
    int lef = 0, rig = min(xx - x + 1, yy - y + 1), mid, ans = 0;
    while (lef <= rig) {
      mid = ((lef + rig) >> 1);
      if (ck(mid))
        ans = mid, lef = mid + 1;
      else
        rig = mid - 1;
    }
    printf("%d\n", ans);
  }
  return 0;
}