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



Sample input:
3 4 4
1101
0110
1101
1 1 3 4
1 1 3 1
2 2 3 4
1 2 2 4


Sample output:
3
2
2
2

Submission status: Runtime Error

Solution code:
import numpy as np

def search(xt, yt, a):
	if a[xt, yt] == '1':
		a[xt, yt] = '2'
		search(xt - 1, yt, a)
		search(xt + 1, yt, a)
		search(xt, yt - 1, a)
		search(xt, yt + 1, a)

n, m, q = map(int, raw_input().split())

arr = []

for i in range(n):
	s = raw_input()
	arr.append(list(s))

arr_np = np.array(arr)

for i in range(q):
	y1, x1, y2, x2 = map(int, raw_input().split())
	arr_temp = arr_np[y1 - 1:y2, x1 - 1:x2]

	a = np.array([0] * (x2 - x1 + 1))
	arr_temp = np.vstack((a, arr_temp))
	arr_temp = np.vstack((arr_temp, a))

	b = np.array([0] * (y2 - y1 + 3))
	arr_temp = np.column_stack([b, arr_temp])
	arr_temp = np.column_stack([arr_temp, b])

	p = np.where(arr_temp == '1')
	s = 0
	while len(p[0]) >= 1:
		search(p[0][0], p[1][0], arr_temp)
		s += 1
		p = np.where(arr_temp == '1')

	print s

Submission status: Runtime Error

Solution code:
N, M, Q = list(map(int, input().split()))
S = [list(map(int, list(input()))) for i in range(N)]

dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]


def dfs(_x, _y):
    target_map[_x][_y] = 0
    for l in range(0, 4):
        nx = _x + dx[l]
        ny = _y + dy[l]
        if 0 <= nx <= tmp_N and 0 <= ny <= tmp_M and target_map[nx][ny] == 1:
            dfs(nx, ny)


for i in range(Q):
    x1, y1, x2, y2 = list(map(int, input().split()))

    target_map = []
    for j in range(x1 - 1, x2):
        tmp_list = []
        for i in range(y1 - 1, y2):
            tmp_list.append(S[j][i])
        target_map.append(tmp_list)

    res = 0
    tmp_N = x2 - x1
    tmp_M = y2 - y1

    for j in range(0, tmp_N + 1):
        for k in range(0, tmp_M + 1):
            if target_map[j][k] == 1:
                dfs(j, k)
                res += 1

    print(res)