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.
There are multiple test cases.
The first line of each test case contains two positive integers N
, 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.
1 ≤ N, M
1 ≤ amount
One line for each test case the least amount of TNT needed to disconnect (0, 0) and (N, M).
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