Problem:
Score : 700 points

Problem Statement

Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right.
Each square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white.
For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once.

Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region?

Process all the queries.

Constraints

1 ≤ N,M ≤ 2000

1 ≤ Q ≤ 200000

S_{i,j} is either 0 or 1.

S_{i,j} satisfies the condition explained in the statement.

1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q)

1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q)

Input

The input is given from Standard Input in the following format:

N M Q
S_{1,1}..S_{1,M}
:
S_{N,1}..S_{N,M}
x_{1,1} y_{i,1} x_{i,2} y_{i,2}
:
x_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2}

Output

For each query, print the number of the connected components consisting of blue squares in the region.

Sample Input 1

3 4 4
1101
0110
1101
1 1 3 4
1 1 3 1
2 2 3 4
1 2 2 4

Sample Output 1

3
2
2
2

In the first query, the whole grid is specified. There are three components consisting of blue squares, and thus 3 should be printed.

In the second query, the region within the red frame is specified. There are two components consisting of blue squares, and thus 2 should be printed.
Note that squares that belong to the same component in the original grid may belong to different components.

Sample Input 2

5 5 6
11010
01110
10101
11101
01010
1 1 5 5
1 2 4 5
2 3 3 4
3 3 3 3
3 1 3 5
1 1 3 4

Sample Output 2

3
2
1
1
3
2

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		// ◆縦の個数、横の個数、1桁数字が連続
		//	3 3
		//	123
		//	234
		//	345

		// 1行、2つ(複数)数字
		String[] ss0 = br.readLine().trim().split(" ", 0);
		int H = Integer.parseInt(ss0[0]);
		int W = Integer.parseInt(ss0[1]);
		int Q = Integer.parseInt(ss0[2]);

		// 2行目以降、数字が連なる。1桁で区切る
		//番兵君でやる。周りを0で囲む。配列は0で初期化されるので、代入時に+1ずらせばOK
		// 00000
		// 01230
		// 02340
		// 03450
		// 00000
		String s = "";
		int kabe = 1;
		int[][] ii = new int[H+(kabe*2)][W+(kabe*2)];  //0で初期化される

		for(int i = 0; i < H; i++){
			s = br.readLine();
			for(int j = 0; j < W; j++){
				if(s.charAt(j)=='1') { ii[i+kabe][j+kabe] = 1; }
			}
		}
					////入力値確認
					//checkInput(ii);

		int x1;
		int x2;
		int y1;
		int y2;
		int a;
		int h;
		int w;
		StringBuilder sb = new StringBuilder();

		for(int q=0; q<Q; q++) {
			x1=0; x2=0; y1=0; y2=0;
			a=0; h=0; w=0;

			ss0 = br.readLine().trim().split(" ", 0);
			x1 = Integer.parseInt(ss0[0]);
			y1 = Integer.parseInt(ss0[1]);
			x2 = Integer.parseInt(ss0[2]);
			y2 = Integer.parseInt(ss0[3]);
			
//			if((x1==x2)&&(y1==y2)){
//				System.out.print("a");
//				a = ii[x1][y1];
//			} else if(y1==y2){
//				System.out.print("b");
//				int j=y1;
//				for(int i=x1; i<x2; i++){
//					a += ii[i][j];
//					h += ( (ii[i][j]+ii[i+1][j])==2 ) ? 1 : 0;
//				}
//			} else if(x1==x2){
//				System.out.print("c");
//				int i=x1;
//				for(int j=y1; j<y2; j++){
//					a += ii[i][j];
//					w += ( (ii[i][j]+ii[i][j+1])==2 ) ? 1 : 0;
//				}
//			} else {
//				System.out.print("d");
			
				for(int i=x1; i<=x2; i++){
				for(int j=y1; j<=y2; j++){
					a += ii[i][j];
					if(i<x2){ h += ( (ii[i][j]+ii[i+1][j])==2 ) ? 1 : 0; }
					if(j<y2){ w += ( (ii[i][j]+ii[i][j+1])==2 ) ? 1 : 0; }

					//System.out.print("(");
					//System.out.print(i + ", ");
					//System.out.print(j + ", ");
					//System.out.print(a + ", ");
					//System.out.print(h + ", ");
					//System.out.print(w + ") ");
					//System.out.println();
				}
				}
//			}
	//		System.out.print("(");
	//		System.out.print(a + ", ");
	//		System.out.print(h + ", ");
	//		System.out.print(w + ") ");
	//		System.out.println();
			
			//System.out.println(a-h-w);
			sb.append(a-h-w);
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
		return;
	}
	//-----------------------------------------------------------------
	// Debug.Print
	static void dp(String s) {
		System.out.println(s);
	}
	static void dp(StringBuilder s) { dp(s.toString()); }
	static void dp(int i) { dp(String.valueOf(i)); }
	static void dp(long i) { dp(String.valueOf(i)); }

    //-----------------------------------------------------------------
	// 入力値確認
	static void checkInput(int[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(int[] i1: i2){
			for(int i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	}





}