PivotOJ

Road Service 2

시간 제한: 3000ms메모리 제한: 1024MB출처: JOI 2023-2024 본선BOJ 31627

문제

In JOI city, there is a grid-shaped road network consisting of HH infinitely long east-west roads and WW infinitely long north-south roads. Intersection (i,j)(i, j) (1 ≤ i ≤ H, 1 ≤ j ≤ W) is the intersection where the ii-th northernmost east-west road and the jj-th westernmost north-south road cross.

Currently, part of the roads is closed due to poor road conditions. Specifically, the status of the roads is as follows:

  • The segment in the ii-th northernmost east-west road (1 ≤ i ≤ H) connecting intersection (i,j)(i, j) and intersection (i,j+1)(i, j + 1) (1 ≤ j ≤ W - 1) is closed if Ai,j=0A_{i, j} = 0 and passable if Ai,j=1A_{i, j} = 1.
  • The segment in the jj-th westernmost north-south road (1 ≤ j ≤ W) connecting intersection (i,j)(i, j) and intersection (i+1,j)(i + 1, j) (1 ≤ i ≤ H - 1) is closed if Bi,j=0B_{i, j} = 0 and passable if Bi,j=1B_{i, j} = 1.
  • The other part of the roads (the part of roads outside the H×WH \times W intersections) is closed.

President K, the mayor of JOI city, decided to make a repair plan of the road network. A repair plan consists of zero or more repairs. A repair is done by choosing an integer ii satisfying 1 ≤ i ≤ H and doing the following:

For every integer jj satisfying 1 ≤ j ≤ W - 1, make the segment in the ii-th northernmost east-west road connecting intersection (i,j)(i, j) and intersection (i,j+1)(i, j + 1) passable (if it is closed).

The repair takes CiC_i days. Note that CiC_i is either 11 or 22.

Since no two repairs in a repair plan can be done in parallel, the period of a repair plan is equal to the sum of the time taken by repairs consisting the repair plan.

President K thinks that securing the route between city facilities is important and asks you QQ questions. The kk-th questions (1 ≤ k ≤ Q) is as follows:

Is there a repair plan that makes TkT_k intersections (Xk,1,Yk,1),(Xk,2,Yk,2),,(Xk,Tk,Yk,Tk)(X_{k,1}, Y_{k,1}), (X_{k,2}, Y_{k,2}), \dots , (X_{k,T_k}, Y_{k,T_k}) mutually reachable? If so, what is the minimum possible period of such a repair plan?

Write a program which, given the status of the road network, the days taken by repairing each east-west road and the details of the questions by President K, answers all the questions.

입력

Read the following data from the standard input.

HH WW QQ

A1,1A1,2A1,W1A_{1,1}A_{1,2} \cdots A_{1,W-1}

A2,1A2,2A2,W1A_{2,1}A_{2,2} \cdots A_{2,W-1}

\vdots

AH,1AH,2AH,W1A_{H,1}A_{H,2} \cdots A_{H,W-1}

B1,1B1,2B1,WB_{1,1}B_{1,2} \cdots B_{1,W}

B2,1B2,2B2,WB_{2,1}B_{2,2} \cdots B_{2,W}

\vdots

BH1,1BH1,2BH1,WB_{H-1,1}B_{H-1,2} \cdots B_{H-1,W}

C1C_1 C2C_2 \cdots CHC_H

Query1Query_1

Query2Query_2

\vdots

QueryQQuery_Q

Here, QuerykQuery_k (1 ≤ k ≤ Q) is as follows:

TkT_k

Xk,1X_{k,1} Yk,1Y_{k,1}

Xk,2X_{k,2} Yk,2Y_{k,2}

\vdots

Xk,TkX_{k,T_k} Yk,TkY_{k,T_k}

출력

Write QQ lines to the standard output. In the kk-th line (1 ≤ k ≤ Q), output the minimum possible period, in days, of a repair plan that makes TkT_k intersections (Xk,1,Yk,1),(Xk,2,Yk,2),,(Xk,Tk,Yk,Tk)(X_{k,1}, Y_{k,1}), (X_{k,2}, Y_{k,2}), \dots , (X_{k,T_k}, Y_{k,T_k}) mutually reachable if such a repair plan exists. Otherwise, output -1.

예제

예제 1

입력
4 3 4
00
00
00
00
100
001
000
1 1 1 1
2
1 1
3 3
2
3 1
1 2
2
2 3
3 3
2
4 2
3 2
출력
1
3
0
-1

예제 2

입력
4 4 4
100
110
011
010
0010
1001
0101
1 1 1 1
2
1 2
3 1
2
1 4
4 1
2
3 2
1 2
2
4 3
1 1
출력
1
3
2
2

예제 3

입력
7 3 3
10
00
00
10
00
01
00
110
101
011
001
110
100
1 1 1 1 1 1 1
3
7 2
3 1
3 2
3
3 1
6 3
2 3
7
2 2
1 3
7 3
5 2
1 2
7 2
3 1
출력
3
2
4

예제 4

입력
4 3 3
00
00
10
00
110
011
001
1 2 2 2
2
1 1
3 1
2
4 3
2 1
2
4 1
1 3
출력
1
2
5

예제 5

입력
7 3 2
01
00
00
00
00
10
01
100
110
011
001
101
001
1 1 2 1 1 2 2
3
7 2
1 3
5 1
5
1 1
2 2
3 1
2 3
4 2
출력
4
1
코드를 제출하려면 로그인하세요.