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 C# (Mono 4.6.2.0) (judge verdict: Memory Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
static class Program{
	const int mod=(int)1e9+7;
		static void Main(){
		Sc sc=new Sc();
		int[] s=sc.Ia;
		int[][][] h=new int[s[0]+1][][];
		h[0]=new int[s[1]+1][];
		string[] g=new string[2];
		g[0]=new string('0',s[1]+1);
		for(int j = 0;j<=s[1];j++) {h[0][j]=new int[3];}
		for(int i = 1;i<=s[0];i++) {
			h[i]=new int[s[1]+1][];
			g[i%2]="0"+sc.S;
			h[i][0]=new int[3];
			for(int j = 1;j<=s[1];j++) {
				h[i][j]=new int[]{h[i][j-1][0]+g[i%2][j]-'0',h[i][j-1][1],h[i-1][j][2]};
				if(g[i%2][j]=='1'){
					if(g[i%2][j-1]==g[i%2][j]){h[i][j][1]++;}
					if(g[(i+1)%2][j]==g[i%2][j]){h[i][j][2]++;}
				}
			}
		}
		for(int i = 1;i<=s[0];i++) {
			for(int j = 1;j<=s[1];j++) {
				h[i][j][0]+=h[i-1][j][0];
				h[i][j][1]+=h[i-1][j][1];
				h[i][j][2]+=h[i][j-1][2];
			}
		}
		StringBuilder sb=new StringBuilder();
		for(int i = 0;i<s[2];i++) {
			int[] q=sc.Ia;
			sb.Append(((h[q[2]][q[3]][0]+h[q[0]-1][q[1]-1][0]-h[q[0]-1][q[3]][0]-h[q[2]][q[1]-1][0])-(h[q[2]][q[3]][1]+h[q[0]-1][q[1]][1]-h[q[0]-1][q[3]][1]-h[q[2]][q[1]][1])-(h[q[2]][q[3]][2]+h[q[0]][q[1]-1][2]-h[q[0]][q[3]][2]-h[q[2]][q[1]-1][2]))+"\n");
		}
		Console.Write(sb);
	}
}

public class Sc{
	private string[] a;
	private int n=-1,m=0;
	public int I{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return int.Parse(a[n]);}}
	public long L{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return long.Parse(a[n]);}}
	public double D{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return double.Parse(a[n]);}}
	public string S{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return a[n];}}
	public int[] Ia{get{if(n!=-1&&n!=a.Length-1){m=n;n=-1;return Array.ConvertAll(a.Skip(m+1).ToArray(),int.Parse);}return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()).Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
}