#P9276. Inheritance Field Maximization

    ID: 22431 Type: Default 1000ms 256MiB

Inheritance Field Maximization

Inheritance Field Maximization

You have inherited a rectangular field from your great-great-grandfather that has \(N\) rows and \(M\) columns. You plan to continue your ancestor's legacy by planting one of two types of seeds: wheat and sunflower.

In each cell \((i,j)\) of the field, planting wheat gives you \(A[i][j]\) euros while planting sunflower gives you \(B[i][j]\) euros. Unfortunately, if two adjacent cells (sharing a side) are planted with different types of seeds, their roots will tangle, incurring a very high repair cost.

Since the repair cost is prohibitive, you decide to plant the same type of seed in every cell. Your task is to determine the maximum amount of money you can earn from the field by choosing the optimal seed uniformly across the entire field.

inputFormat

The first line of input contains two integers \(N\) and \(M\) \( (1 \leq N, M \leq 1000)\), representing the number of rows and columns of the field.

The next \(N\) lines each contain \(M\) integers, representing the matrix \(A\) where \(A[i][j]\) is the amount of euros if wheat is planted at cell \((i,j)\).

The following \(N\) lines each contain \(M\) integers, representing the matrix \(B\) where \(B[i][j]\) is the amount of euros if sunflower is planted at cell \((i,j)\).

outputFormat

Output a single integer, the maximum amount of money that can be obtained by uniformly planting one type of seed in every cell.

sample

2 2
1 2
3 4
2 2
2 2
10