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 JAVA):

Solution:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;


public class D {

	static StringTokenizer st;
	static BufferedReader br;
	static PrintWriter pw;
	static int[][]dp;
	static int[][][]t;
	static int n, m;
	static int[]log_2;
	static int yy1, yy2;
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		n = nextInt();
		m = nextInt();
		int[][]a = new int[n+1][m+1];
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				a[i][j] = nextInt();
			}
		}
		log_2 = new int[1001];
		for (int i = 1; i <= m; i++) {
			while ((1 << (log_2[i]+1)) <= i)
				log_2[i]++;
		}
		dp = new int[n+1][m+1];
		t = new int[11][m+1][4*n];
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				if (a[i][j]==1)
					dp[i][j] = Math.min(dp[i-1][j-1], Math.min(dp[i-1][j], dp[i][j-1])) + 1;
			}
		}
		for (int i = 0; i <= 10; i++) {
			for (int j = 1; j <= m; j++) {
				Buildx(1, 1, n, j, i);
			}
		}
		int q = nextInt();
		while (q-->0) {
			int x1 = nextInt();
			int y1 = nextInt();
			int x2 = nextInt();
			int y2 = nextInt();
			int left = 0, right = Math.min(x2-x1, y2-y1) + 2;
			while (right-left > 1) {
				int mid = (left+right) >> 1;
				yy1 = y1+mid-1;
				yy2 = y2;
				if (getMax(1, 1, n, x1+mid-1, x2) >= mid)
					left = mid;
				else
					right = mid;
			}
			pw.println(left);
		}
		pw.close();
	}
	
	private static int getMax(int v, int tl, int tr, int x1, int x2) {
		if (x1 > x2)
			return -1;
		if (tl==x1 && tr==x2) {
			if (yy1 > yy2)
				return -1;
			int k = log_2[yy2-yy1+1];
			return Math.max(t[k][yy1][v], t[k][yy2-(1 << k)+1][v]);
		}
		int tm = (tl+tr) >> 1;
		return Math.max(getMax(2*v, tl, tm, x1, Math.min(x2, tm)), getMax(2*v+1, tm+1, tr, Math.max(x1, tm+1), x2));
	}
	
	private static void Buildx(int v, int tl, int tr, int y, int k) {
		if (tl==tr) {
			if (y+(1 << k)-1 > m)
				return;
			if (k==0)
				t[k][y][v] = dp[tl][y];
			else
				t[k][y][v] = Math.max(t[k-1][y][v], t[k-1][y+(1 << (k-1))][v]);	
		}
		else {
			int tm = (tl+tr) >> 1;
			Buildx(2*v, tl, tm, y, k);
			Buildx(2*v+1, tm+1, tr, y, k);
			t[k][y][v] = Math.max(t[k][y][2*v], t[k][y][2*v+1]);
		}
	}
	
	private static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}
	private static long nextLong() throws IOException {
		return Long.parseLong(next());
	}
	private static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
	private static String next() throws IOException {
		while (st==null || !st.hasMoreTokens())
			st = new StringTokenizer(br.readLine());
		return st.nextToken();
	}
}