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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define pr(...) cerr<< GET_MACRO(__VA_ARGS__,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__) <<endl
#define pr1(a) (#a)<<"="<<(a)<<" "
#define pr2(a,b) pr1(a)<<pr1(b)
#define pr3(a,b,c) pr1(a)<<pr2(b,c)
#define pr4(a,b,c,d) pr1(a)<<pr3(b,c,d)
#define pr5(a,b,c,d,e) pr1(a)<<pr4(b,c,d,e)
#define pr6(a,b,c,d,e,f) pr1(a)<<pr5(b,c,d,e,f)
#define pr7(a,b,c,d,e,f,g) pr1(a)<<pr6(b,c,d,e,f,g)
#define pr8(a,b,c,d,e,f,g,h) pr1(a)<<pr7(b,c,d,e,f,g,h)
using namespace std;
using Int = long long;
using ll = long long;
using Double = long double;
const Int INF = (1LL<<55)+1e9; // ~ 3.6 * 1e16
const Int mod = (1e9)+7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int,Int>;
using T = tuple<Int,Int,Int>;
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
ostream& operator<<(ostream& o,P p){return o<<"("<<p.first<<","<<p.second<<")";}
ostream& operator<<(ostream& o,T t){return o<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<")";}
istream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}
ostream& operator<<(ostream& o,vector<auto> &a){Int i=0;for(auto t:a)o<<(i++?" ":"")<<t;return o;}
istream& operator>>(istream& i,vector<auto> &a){for(auto &t:a)i>>t;return i;}
void prArr(auto a,string s=" "){Int i=0;for(auto t:a)cout<<(i++?s:"")<<t;cout<<endl;}

class CumulativeSum2D{
public:
  Int h,w;
  vector<vector<Int> > sum;
  CumulativeSum2D(){}
  CumulativeSum2D(vector<vector<Int> > A){
    assert(A.size());
    h = A.size();
    w = A[0].size();
    sum.resize(h+1,vector<Int>(w+1));
    for(Int i=1;i<=h;i++)
      for(Int j=1;j<=w;j++) sum[i][j] += sum[i][j-1] + A[i-1][j-1];

    for(Int j=1;j<=w;j++)
      for(Int i=1;i<=h;i++) sum[i][j] += sum[i-1][j];
    
  }
  
  //半開区間
  Int get(Int x,Int y,Int X,Int Y){
    assert(x <= X && y <= Y);
    assert(0<=x && x<=w && 0<=X && x <= w);
    assert(0<=y && y<=h && 0<=Y && Y <= h);
    return sum[Y][X] - sum[y][X] - sum[Y][x] + sum[y][x];
  }
};


signed main(){
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  Int h,w,Q;
  cin>>h>>w>>Q;

  vector<string > mp(h);
  cin>>mp;

  CumulativeSum2D sumNode;
  {
    vector<vector<Int> > tmp(h, vector<Int>(w));
    for(Int i=0;i<h;i++)
      for(Int j=0;j<w;j++) tmp[i][j] = mp[i][j] - '0';
    sumNode = CumulativeSum2D(tmp);
  }

  CumulativeSum2D sumEdgeW;
  {
    vector<vector<Int> > tmp(h+1,vector<Int>(w+1));
    for(Int i=0;i<h;i++)
      for(Int j=0;j<w-1;j++)
	if(mp[i][j] == '1' && mp[i][j+1] == '1') tmp[i][j] = 1;
    sumEdgeW = CumulativeSum2D(tmp);
  }

  CumulativeSum2D sumEdgeH;
  {
    vector<vector<Int> > tmp(h+1, vector<Int> (w+1));
    for(Int i=0;i<h-1;i++)
      for(Int j=0;j<w;j++)
	if(mp[i][j] == '1' && mp[i+1][j] == '1') tmp[i][j] = 1;
    sumEdgeH = CumulativeSum2D(tmp);
  }


  while(Q--){
    Int x1, y1, x2, y2;
    cin>>y1>>x1>>y2>>x2;
    x1--,y1--,x2--,y2--;
    
    Int nodes = sumNode.get(x1,y1,x2+1,y2+1);
    Int edgew = sumEdgeW.get(x1,y1,x2,y2+1);
    Int edgeh = sumEdgeH.get(x1,y1,x2+1,y2);
    Int edges = edgew + edgeh;
    Int ans = nodes - edges;
    cout<<ans<<endl;
  }
  
  
  return 0;
}