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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using static Atcoder;
using System.Threading.Tasks;
using System.ComponentModel;

public class Hello
{
    private const int INF = 1000000007;
    public static void Main()
    {
        int[] inp = IntLine();
        int N = inp[0], M = inp[1], Q = inp[2];
        int[,] field = new int[N, M];
        for (int i = 0; i < N; i++)
        {
            int[] b = Console.ReadLine().ToCharArray().Select(c => int.Parse(c.ToString())).ToArray();
            for (int j = 0; j < M; j++) field[i, j] = b[j];
        }
        int[,] Qs = new int[Q, 4];
        for (int i = 0; i < Q; i++)
        {
            int[] b = IntLine().Select(v => v - 1).ToArray();
            for (int j = 0; j < 4; j++) Qs[i, j] = b[j];
        }

        CumulativeSum node = new CumulativeSum(N, M);
        Array.Copy(field, node.Info, field.Length);
        node.Run();
        CumulativeSum e1 = new CumulativeSum(N, M - 1);
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M - 1; j++)
            {
                if (field[i, j] == 1 && field[i, j + 1] == 1) e1.Info[i, j] = 1;
            }
        }
        e1.Run();
        CumulativeSum e2 = new CumulativeSum(N - 1, M);
        for (int i = 0; i < N - 1; i++)
        {
            for (int j = 0; j < M; j++)
            {
                if (field[i, j] == 1 && field[i + 1, j] == 1) e2.Info[i, j] = 1;
            }
        }
        e2.Run();

        for (int i = 0; i < Q; i++)
        {
            int y1 = Qs[i, 0], x1 = Qs[i, 1], y2 = Qs[i, 2], x2 = Qs[i, 3];
            int ns = node.Info[y2, x2];
            int es = 0;
            int yc = (y2 - y1);
            int xc = (x2 - x1);
            if (x1 != 0 && y1 != 0)
            {
                ns += -node.Info[y2, x1 - 1] - node.Info[y1 - 1, x2] + node.Info[y1 - 1, x1 - 1];
            }
            else if (y1 != 0)
            {
                ns += -node.Info[y1 - 1, x2];
            }
            else if (x1 != 0)
            {
                ns += -node.Info[y2, x1 - 1];
            }

            if (xc > 0) { es += e1.Info[y2, x2 - 1]; }
            if (yc > 0) { es += e2.Info[y2 - 1, x2]; }
            if (x1 != 0 && y1 != 0 && xc > 0 && yc > 0)
            {
                es += -e1.Info[y2, x1 - 1] - e1.Info[y1 - 1, x2 - 1] + e1.Info[y1 - 1, x1 - 1];
                es += -e2.Info[y2 - 1, x1 - 1] - e2.Info[y1 - 1, x2] + e2.Info[y1 - 1, x1 - 1];
            }
            else if (x1 != 0)
            {
                if (xc == 0 && yc > 0) { es -= e2.Info[y2 - 1, x1 - 1]; }
                else if (yc > 0) { es -= e1.Info[y2, x1 - 1] + e2.Info[y2 - 1, x1 - 1]; }
                else if (y1 == 0) es -= e1.Info[y2, x1 - 1];
            }
            else if (y1 != 0)
            {
                if (yc == 0 && xc > 0) { es -= e1.Info[y1 - 1, x2 - 1]; }
                else if (xc > 0) { es -= e2.Info[y1 - 1, x2] + e1.Info[y1 - 1, x2 - 1]; }
                else if(x1 == 0) es -= e2.Info[y1 - 1, x2];
            }
            Console.WriteLine(ns - es);
        }
    }
}

public class CumulativeSum
{
    public int[,] Info { get; set; }

    public CumulativeSum(int N, int M)
    {
        Info = new int[N, M];
    }

    public void Run()
    {
        RightAdd();
        DownAdd();
    }

    private void RightAdd()
    {
        for (int i = 0; i < Info.GetLength(0); i++)
        {
            for (int j = 1; j < Info.GetLength(1); j++)
            {
                Info[i, j] += Info[i, j - 1];
            }
        }
    }

    private void DownAdd()
    {
        for (int i = 1; i < Info.GetLength(0); i++)
        {
            for (int j = 0; j < Info.GetLength(1); j++)
            {
                Info[i, j] += Info[i - 1, j];
            }
        }
    }
}

public class Atcoder
{
    public static void Swap<T>(ref T a, ref T b)
    {
        var t = a;
        a = b;
        b = t;
    }
    public static int GCD(int a, int b)
    {
        if (a < b) Swap(ref a, ref b);
        int d = a % b;
        while (b != 0)
        {
            d = a % b;
            a = b;
            b = d;
        }
        return a;
    }
    public static long LCM(int a, int b)
    {
        return (long)a * b / GCD(a, b);
    }
    public static string StrReverce(string s)
    {
        return new string(s.Reverse().ToArray());
    }
    public static int IntRead()
    {
        return int.Parse(Console.ReadLine());
    }
    public static bool IsPrime(int n)
    {
        if (n < 2) return false;
        if (n == 2) return true;
        if (n % 2 == 0) return false;

        //double sqrtNum = Math.Sqrt(n);
        for (int i = 3; i < n; i += 2)
        {
            if (n % i == 0) return false;
        }
        return true;
    }
    public static string[] SpRead(char c)
    {
        return Console.ReadLine().Split(c);
    }
    public static long[] LSRead()
    {
        return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
    }
    public static long Combination(int n, int r)
    {
        if (n == r) return 1;
        return
            Enumerable.Range((n - r + 1), r).ToList().Aggregate((now, next) => now * next) /
            Enumerable.Range(1, r).ToList().Aggregate((now, next) => now * next);
    }
    public static long Permutation(int n, int r)
    {
        if (n == r) return 1;
        return
            Enumerable.Range((n - r), r).ToList().Aggregate((now, next) => now * next);
    }
    public static long LongRead()
    {
        return long.Parse(Console.ReadLine());
    }
    public static long[] LongLine()
    {
        return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
    }
    public static int[] IntLine()
    {
        return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    }
}