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.*;
import java.util.*;

public class AnimalsandPuzzle {

	static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

	static int n, m;
	static int[][] a;
	static int[][][][] st;
	
	static void build(int[][] dp) {
		for (int ir = 0; ir < n; ir ++) {
			for (int ic = 0; ic < m; ic++) {
				st[0][ir][0][ic] = dp[ir][ic];
			}
			
			for (int jc = 1; 1 << jc <= m; jc ++) {
				for (int ic = 0; ic + (1 << jc) - 1 < m; ic++) {
					st[0][ir][jc][ic] = Math.max(st[0][ir][jc - 1][ic], st[0][ir][jc - 1][ic + (1 << (jc - 1))]);
				}
			}
		}
		
		for (int jr = 1; 1 << jr <= n; jr++) {
			for (int ir = 0; ir + (1 << jr) - 1 < n; ir++) {
				for (int jc = 0; 1 << jc <= m; jc++) {
					for (int ic = 0; ic + (1 << jc) -1 < m; ic++) {
						int a2 = st[jr - 1][ir][jc][ic];
						int b = st[jr - 1][ir + (1 << (jr - 1))][jc][ic];
						st[jr][ir][jc][ic] = Math.max(a2, b);
					}
				}
			}
		}
	}
	
	static int query(int x1, int y1, int x2, int y2) {
		int kx = log2(x2 - x1 + 1), ky = log2(y2 - y1 + 1);
		int m1 = Math.max(st[kx][x1][ky][y1], st[kx][x1][ky][y2 - (1 << ky) + 1]);
		int m2 = Math.max(st[kx][x2 - (1 << kx) + 1][ky][y1], st[kx][x2 - (1 << kx) + 1][ky][y2 - (1 << ky) + 1]);
		return Math.max(m1, m2);
	}
	
	static int log2(int n) {
		return Integer.numberOfTrailingZeros(Integer.highestOneBit(n));
	}
	
	static void init() {
		int[][] dp = new int[n][m];
		for (int i = 0; i < n; i++) {
			dp[i][0] = a[i][0];
		}
		for (int i = 0; i < m; i++) {
			dp[0][i] = a[0][i];
		}
		
		for (int i = 1; i < n; i++) {
			for (int j = 1; j < m; j++) {
				dp[i][j] = a[i][j] == 0 ? 0 : Math.min(Math.min(dp[i-1][j-1], dp[i-1][j]), dp[i][j-1]) + 1;
			}
		}
		st = new int[log2(n) + 1][n + 1][log2(m) + 1][m];
		build(dp);
	}
	
	static void process(int x1, int y1, int x2, int y2) throws Exception {
		int left = 0, right = Math.min(x2 - x1, y2 - y1);
		int max = 0;
		while (left <= right) {
			int mid = left + (right - left) / 2;
			int query = query(x1 + mid, y1 + mid, x2, y2);
			if (query >= mid + 1) {
				max = mid + 1;
				left = mid + 1;
			} else {
				right = mid - 1;
			}
		}
		writer.write(String.format("%d", max));
		writer.newLine();
	}

	public static void main(String[] args) throws Exception {
		//		writer = new BufferedWriter(new FileWriter("output.txt"));
		String[] data = reader.readLine().split(" ");
		n = Integer.parseInt(data[0]);
		m = Integer.parseInt(data[1]);
		a = new int[n][];
		for (int i = 0; i < n; i++) {
			a[i] = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
		}
		init();
		int t = Integer.parseInt(reader.readLine());
		while (t-- != 0) {
			data = reader.readLine().split(" ");
			process(Integer.parseInt(data[0]) - 1, Integer.parseInt(data[1]) - 1, Integer.parseInt(data[2]) - 1, Integer.parseInt(data[3]) - 1);
//			writer.flush();
		}
		writer.close();
		reader.close();
	}

}