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>
template <typename T, size_t N>
struct MakeVector {
  template <typename... Args,
            typename R = std::vector<decltype(MakeVector<T, N - 1>::make_vector(
                std::declval<Args>()...))>>
  static R make_vector(std::size_t first, Args... sizes) {
    auto inner = MakeVector<T, N - 1>::make_vector(sizes...);
    return R(first, inner);
  }
};
template <typename T>
struct MakeVector<T, 1> {
  template <typename R = std::vector<T>>
  static R make_vector(std::size_t size, const T& value) {
    return R(size, value);
  }
};
template <typename T, typename... Args>
auto make_vector(Args... args)
    -> decltype(MakeVector<T, sizeof...(Args) - 1>::make_vector(args...)) {
  return MakeVector<T, sizeof...(Args) - 1>::make_vector(args...);
}
template <typename T>
class IntegerIterator
    : public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T> {
 public:
  explicit IntegerIterator(T value) : value(value) {}
  IntegerIterator& operator++() {
    ++value;
    return *this;
  }
  IntegerIterator operator++(int) {
    IntegerIterator copy = *this;
    ++value;
    return copy;
  }
  IntegerIterator& operator--() {
    --value;
    return *this;
  }
  IntegerIterator operator--(int) {
    IntegerIterator copy = *this;
    --value;
    return copy;
  }
  T operator*() const { return value; }
  bool operator==(IntegerIterator rhs) { return value == rhs.value; }
  bool operator!=(IntegerIterator rhs) { return !(*this == rhs); }

 private:
  T value;
};
template <typename T>
class IntegerRange {
 public:
  IntegerRange(T begin, T end) : begin_(begin), end_(end) { ; }
  IntegerIterator<T> begin() const { return IntegerIterator<T>(begin_); }
  IntegerIterator<T> end() const { return IntegerIterator<T>(end_); }

 private:
  T begin_;
  T end_;
};
template <typename T>
class ReversedIntegerRange {
 public:
  ReversedIntegerRange(T begin, T end) : begin_(begin), end_(end) { ; }
  std::reverse_iterator<IntegerIterator<T>> begin() const {
    return std::reverse_iterator<IntegerIterator<T>>(
        IntegerIterator<T>(begin_));
  }
  std::reverse_iterator<IntegerIterator<T>> end() const {
    return std::reverse_iterator<IntegerIterator<T>>(IntegerIterator<T>(end_));
  }

 private:
  T begin_;
  T end_;
};
template <typename T>
IntegerRange<T> range(T to) {
  return IntegerRange<T>(0, to);
}
template <typename T>
IntegerRange<T> range(T from, T to) {
  return IntegerRange<T>(from, to);
}
template <typename T>
IntegerRange<T> inclusiveRange(T to) {
  return IntegerRange<T>(0, to + 1);
}
template <typename T>
IntegerRange<T> inclusiveRange(T from, T to) {
  return IntegerRange<T>(from, to + 1);
}
template <typename T>
ReversedIntegerRange<T> downrange(T from) {
  return ReversedIntegerRange<T>(from, 0);
}
template <typename T>
ReversedIntegerRange<T> downrange(T from, T to) {
  return ReversedIntegerRange<T>(from, to);
}
template <typename T>
ReversedIntegerRange<T> inclusiveDownrange(T from) {
  return ReversedIntegerRange<T>(from + 1, 0);
}
template <typename T>
ReversedIntegerRange<T> inclusiveDownrange(T from, T to) {
  return ReversedIntegerRange<T>(from + 1, to);
}
template <typename T>
std::size_t leastSignificantBit(T number) {
  ;
  for (std::size_t i = 0;; ++i) {
    if ((number >> i) & 1) {
      return i;
    }
  }
}
template <typename T>
bool isPowerOf2(T number) {
  return number != 0 && (number & (number - 1)) == 0;
}
using namespace std;
array<array<array<array<short, 10>, 10>, 1000>, 1000> sparse;
class TaskD {
 public:
  void solve(std::istream& in, std::ostream& out) {
    int n = 1000, m = 1000;
    in >> n >> m;
    auto v = make_vector<int>(n, m, 0);
    for (int i : range(n)) {
      for (int j : range(m)) {
        in >> v[i][j];
      }
    }
    vector<int> logs(1001);
    for (int i : inclusiveRange(2, 1000)) {
      logs[i] = logs[i - 1] + isPowerOf2(i);
    }
    auto square = make_vector<int>(n + 1, m + 1, 0);
    {
      auto right = make_vector<int>(n + 1, m + 1, 0);
      auto down = make_vector<int>(n + 1, m + 1, 0);
      for (int i : downrange(n)) {
        for (int j : downrange(m)) {
          if (v[i][j]) {
            right[i][j] = right[i][j + 1] + 1;
            down[i][j] = down[i + 1][j] + 1;
            square[i][j] =
                min(right[i][j], min(down[i][j], square[i + 1][j + 1] + 1));
          }
        }
      }
    }
    for (int i : downrange(n)) {
      for (int j : downrange(m)) {
        sparse[i][j][0][0] = (short)square[i][j];
        for (int q = 1; j + (1 << q) <= m; ++q) {
          int p = 0;
          sparse[i][j][p][q] = max(sparse[i][j][p][q - 1],
                                   sparse[i][j + (1 << (q - 1))][p][q - 1]);
        }
        for (int p = 1; i + (1 << p) <= n; ++p) {
          sparse[i][j][p][0] = max(sparse[i][j][p - 1][0],
                                   sparse[i + (1 << (p - 1))][j][p - 1][0]);
          for (int q = 1; j + (1 << q) <= m; ++q) {
            sparse[i][j][p][q] = max(sparse[i][j][p][q - 1],
                                     sparse[i][j + (1 << (q - 1))][p][q - 1]);
          }
        }
      }
    }
    auto get = [&](int lx, int ly, int rx, int ry) {
      int logx = logs[rx - lx];
      int logy = logs[ry - ly];
      int ans = 0;
      for (int x : {lx, rx - (1 << logx)}) {
        for (int y : {ly, ry - (1 << logy)}) {
          ans = max(ans, (int)sparse[x][y][logx][logy]);
        }
      }
      return ans;
    };
    int q = 1000000;
    in >> q;
    for (int _ : range(q)) {
      int lx, ly, rx, ry;
      in >> lx >> ly >> rx >> ry;
      --lx, --ly;
      int l = 0;
      int r = min(ry - ly, rx - lx) + 1;
      while (r - l > 1) {
        int M = (l + r) / 2;
        if (get(lx, ly, rx - M + 1, ry - M + 1) >= M) {
          l = M;
        } else {
          r = M;
        }
      }
      out << l << "\n";
    }
  }
};
int main() {
  std::ios_base::sync_with_stdio(false);
  TaskD solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  in.tie(0);
  out << std::fixed;
  out.precision(20);
  solver.solve(in, out);
  return 0;
}