Road Service 2
문제
In JOI city, there is a grid-shaped road network consisting of infinitely long east-west roads and infinitely long north-south roads. Intersection (1 ≤ i ≤ H, 1 ≤ j ≤ W) is the intersection where the -th northernmost east-west road and the -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 -th northernmost east-west road (1 ≤ i ≤ H) connecting intersection and intersection (1 ≤ j ≤ W - 1) is closed if and passable if .
- The segment in the -th westernmost north-south road (1 ≤ j ≤ W) connecting intersection and intersection (1 ≤ i ≤ H - 1) is closed if and passable if .
- The other part of the roads (the part of roads outside the 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 satisfying 1 ≤ i ≤ H and doing the following:
For every integer satisfying 1 ≤ j ≤ W - 1, make the segment in the -th northernmost east-west road connecting intersection and intersection passable (if it is closed).
The repair takes days. Note that is either or .
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 questions. The -th questions (1 ≤ k ≤ Q) is as follows:
Is there a repair plan that makes intersections 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.
Here, (1 ≤ k ≤ Q) is as follows:
출력
Write lines to the standard output. In the -th line (1 ≤ k ≤ Q), output the minimum possible period, in days, of a repair plan that makes intersections 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