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 Rust (1.15.1) (judge verdict: Accepted)

macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( iter = $ iter : ident , $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut $ iter = s . split_whitespace ( ) ; input_inner ! { $ iter , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut iter = s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; }
macro_rules ! input_inner { ( $ iter : expr ) => { } ; ( $ iter : expr , ) => { } ; ( $ iter : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , mut $ var : usize $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , $ var : usize $ ( $ r ) * } } ; }
macro_rules ! read_value { ( $ iter : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ iter , $ t ) ) ,* ) } ; ( $ iter : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ iter , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ iter : expr , { chars : $ base : expr } ) => { read_value ! ( $ iter , String ) . chars ( ) . map ( | c | ( c as u8 - $ base as u8 ) as usize ) . collect ::< Vec < usize >> ( ) } ; ( $ iter : expr , { char : $ base : expr } ) => { read_value ! ( $ iter , { chars : $ base } ) [ 0 ] } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , char ) => { read_value ! ( $ iter , chars ) [ 0 ] } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
#[inline]
fn rmb(k: usize) -> usize {
    (k as isize & -(k as isize)) as usize
}
pub trait Monoid {
    type T: Clone + PartialEq;
    fn mempty(&self) -> Self::T;
    fn mappend(&self, x: &Self::T, y: &Self::T) -> Self::T;
}
#[derive(Clone, Debug)]
pub struct BinaryIndexedTree2D<M: Monoid> {
    h: usize,
    w: usize,
    bit: Vec<Vec<M::T>>,
    m: M,
}
impl<M: Monoid> BinaryIndexedTree2D<M> {
    pub fn new(h: usize, w: usize, m: M) -> BinaryIndexedTree2D<M> {
        let bit = vec![vec![m.mempty(); w + 1]; h + 1];
        BinaryIndexedTree2D {
            h: h,
            w: w,
            bit: bit,
            m: m,
        }
    }
    #[doc = " 1-indexed"]
    pub fn query(&self, i: usize, j: usize) -> M::T {
        let mut res = self.m.mempty();
        let mut a = i;
        while a > 0 {
            let mut b = j;
            while b > 0 {
                res = self.m.mappend(&res, &self.bit[a][b]);
                b -= rmb(b);
            }
            a -= rmb(a);
        }
        res
    }
    #[doc = " 1-indexed"]
    pub fn update(&mut self, i: usize, j: usize, x: M::T) {
        let mut a = i;
        while a <= self.h {
            let mut b = j;
            while b <= self.w {
                self.bit[a][b] = self.m.mappend(&self.bit[a][b], &x);
                b += rmb(b);
            }
            a += rmb(a);
        }
    }
}
pub trait Group: Monoid {
    fn invert(&self, x: &Self::T) -> Self::T;
}
impl<G: Group> BinaryIndexedTree2D<G> {
    #[doc = " 0-indexed [i1, i2) x [j1, j2)"]
    pub fn query_section(&self, i1: usize, j1: usize, i2: usize, j2: usize) -> G::T {
        let mut res = self.m.mempty();
        res = self.m.mappend(&res, &self.query(i1, j1));
        res = self.m.mappend(&res, &self.m.invert(&self.query(i1, j2)));
        res = self.m.mappend(&res, &self.m.invert(&self.query(i2, j1)));
        res = self.m.mappend(&res, &self.query(i2, j2));
        res
    }
    #[doc = " 1-indexed"]
    pub fn get_val(&self, i: usize, j: usize) -> G::T {
        self.query_section(i - 1, j - 1, i, j)
    }
    #[doc = " 1-indexed"]
    pub fn set_val(&mut self, i: usize, j: usize, x: G::T) {
        let y = self.m.invert(&self.get_val(i, j));
        let z = self.m.mappend(&y, &x);
        self.update(i, j, z);
    }
}
#[derive(Clone, Debug)]
pub struct AnyGroup<T: Clone, F: Fn(&T, &T) -> T, G: Fn(&T) -> T> {
    id: T,
    op: F,
    inv: G,
}
impl<T: Clone, F: Fn(&T, &T) -> T, G: Fn(&T) -> T> AnyGroup<T, F, G> {
    pub fn new(id: T, op: F, inv: G) -> AnyGroup<T, F, G> {
        AnyGroup {
            id: id,
            op: op,
            inv: inv,
        }
    }
}
impl<T: Clone + PartialEq, F: Fn(&T, &T) -> T, G: Fn(&T) -> T> Monoid for AnyGroup<T, F, G> {
    type T = T;
    fn mempty(&self) -> T {
        self.id.clone()
    }
    fn mappend(&self, x: &T, y: &T) -> T {
        (self.op)(x, y)
    }
}
impl<T: Clone + PartialEq, F: Fn(&T, &T) -> T, G: Fn(&T) -> T> Group for AnyGroup<T, F, G> {
    fn invert(&self, x: &T) -> T {
        (self.inv)(x)
    }
}
fn main() {
    input! { n, m, q, s: [{chars: '0'}; n], xyxy: [(usize1,usize1,usize1,usize1); q] };
    let mut bit = BinaryIndexedTree2D::new(n * 2, m * 2, AnyGroup::new(0, |&x, &y| x + y, |&x| -x));
    for i in 0..n {
        for j in 0..m {
            if s[i][j] == 1 {
                bit.update(i * 2 + 1, j * 2 + 1, 1);
            }
            if i + 1 < n && s[i][j] == 1 && s[i + 1][j] == 1 {
                bit.update((i + 1) * 2, j * 2 + 1, -1);
            }
            if j + 1 < m && s[i][j] == 1 && s[i][j + 1] == 1 {
                bit.update(i * 2 + 1, (j + 1) * 2, -1);
            }
        }
    }
    for &(x1, y1, x2, y2) in &xyxy {
        let ans = bit.query_section(x1 * 2, y1 * 2, x2 * 2 + 1, y2 * 2 + 1);
        println!("{}", ans);
    }
}