Tianjin University Online Judge
Contests Virtual Contests Problems Submit Runs Status Rank List Forum

3410.   War
Time Limit: 15.0 Seconds   Memory Limit: 65536K
Total Runs: 319   Accepted Runs: 42    Multiple test files



Country X is under attack by enemies. Now the army of enemy has arrived at City Y. City Y consists of an N * M grid. All the paths in the grid are bidirectional, horizontal or vertical or diagonal. The upper-left corner is (0, 0) and lower-right corner is (N, M). The army enters at (0, 0) and they must get to (N, M) in order to continue their attack to the capital of Country X. The figure below shows what does City Y looks like.

Every blackened node represents a vertex. The number beside each edge is the amount of TNT needed to destroy that road. The army of Country X is unable to beat the enemy now. The only thing they can do is to prevent them from heading to their capital so that they can have more time to prepare for striking back. Of course they want to use the least amount of TNT to disconnect (0, 0) and (N, M). You are a talented programmer, please help them decide the least amount needed.

Input

There are multiple test cases.

The first line of each test case contains two positive integers N and M, representing height and width of the grid.

Then N+1 lines each containing M integers, giving you the amount needed of horizontal roads in row major order.

Then N lines each containing M+1 integers, giving you the amount needed of vertical roads in row major order.

Then 2N lines each containing 2M integers, giving you the amount needed of diagonal roads in row major order.

There is a blank line after each input block. The sample input is corresponding to the figure above.

Restriction:

1 ≤ N, M ≤ 500

1 ≤ amount ≤ 1,000,000

Output

One line for each test case the least amount of TNT needed to disconnect (0, 0) and (N, M).

Sample Input

2 3
1 9 4
1 8 7
6 2 3
7 5 4 8
6 2 8 7
10 4 1 7 5 3
5 4 10 2 1 9
6 3 2 9 5 3
8 9 6 3 10 10

Sample Output

18



Source: Multi-School Training Contest #13 [Hosted By HIT]
Submit   List    Runs   Forum   Statistics

Tianjin University Online Judge v1.3.0
Maintance:G.D.Retop. Developer: SuperHacker, G.D.Retop