Tube Master II

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

Yuuka is playing "Tube Master". The game field is divided into n×mn \times m cells and (n+1)×(m+1)(n + 1) \times (m + 1) crossings connected by (n+1)×m(n + 1) \times m horizontal tubes and n×(m+1)n \times (m + 1) vertical ones. The cells are conveniently labeled with (i,j)(i, j) for 1in1 \leq i \leq n, 1jm1 \leq j \leq m, and the crossings are labeled with (i,j)(i, j) for 1i(n+1)1 \leq i \leq (n+1), 1j(m+1)1 \leq j \leq (m+1). Additionally, each cell (i,j)(i, j) contains an integer count_i,j\mathit{count}\_{i, j}.

Yuuka decides to use some of the tubes. However, the game poses several weird restrictions.

  1. Either 00 or 22 tubes connected to each crossing are used.
  2. No two consecutive horizontal tubes are used simultaneously, and no consecutive vertical tubes are used simultaneously. Two tubes are consecutive if and only if they share the same crossing.
  3. Exactly count_i,j\mathit{count}\_{i, j} tubes surrounding cell (i,j)(i, j) are used.

Using the tube connecting crossing (i,j)(i, j) and (i,j+1)(i, j + 1) costs a_i,ja\_{i, j}, and using the tube connecting crossing (i,j)(i, j) and (i+1,j)(i + 1, j) costs b_i,jb\_{i, j}. Yuuka would like to find a configuration satisfying the above constrains with the minimum possible total cost.

입력

The input contains zero or more test cases, and is terminated by end-of-file. For each test case:

The first line contains two integers nn and mm (1n,m1001 \leq n, m \leq 100).

The ii-th of the following nn lines contains mm integers count_i,1,count_i,2,,count_i,m\mathit{count}\_{i, 1}, \mathit{count}\_{i, 2}, \dots, \mathit{count}\_{i, m} (0count_i,j40 \leq \mathit{count}\_{i, j} \leq 4).

The ii-th of the next (n+1)(n + 1) lines contains mm integers a_i,1,a_i,2,,a_i,ma\_{i, 1}, a\_{i, 2}, \dots, a\_{i, m}.

The ii-th of the last nn lines contains (m+1)(m + 1) integers b_i,1,b_i,2,,b_i,m+1b\_{i, 1}, b\_{i, 2}, \dots, b\_{i, m + 1}.

The constraints are: 1a_i,j,b_i,j1091 \leq a\_{i, j}, b\_{i, j} \leq 10^9.

It is guaranteed that the total sum of nmn \cdot m in all test cases does not exceed 10410^4.

출력

For each test case, output an integer which denotes the minimum cost of the configuration. If there is no valid configuration, output "-1" instead.