KAIST is running out of budget — they need some money! They thought the dormitories were way too luxurious compared to the other buildings of KAIST; they planned to sell all the dormitory buildings and build a new completely non-aesthetic one.
The new dormitory will have grid shape — it can't be more boring than this — of size $$$N\times M$$$, each cell being a room for the students to live in. We are going to add some windows, because we want students to get some sunlight during the daytime!
We plan to have exactly $$$w_{i, j}$$$ windows in the room $$$(i, j)$$$. Each room is surrounded by $$$4$$$ unit edges of the grid. A window can be built on the side of a unit edge of the grid, and at most one window can be built on each side of a unit edge — so each cell has between $$$0$$$ and $$$4$$$ windows. A window is one-sided: a window on the opposite side of a unit edge does not count as a window in the room.
Unfortunately, students will experience huge discomfort when their private space is watched by someone else through the window. The total discomfort is the number of pairs of students $$$\{a, b\}$$$ such that $$$a$$$ and $$$b$$$ can see each other's private space through the window.
In other words, if a unit edge has windows on both sides, the total discomfort increases by the product of the numbers of people living in the two rooms sharing the window.
You are given $$$w_{i, j}$$$, the number of windows planned for the room $$$(i, j)$$$, and $$$p_{i, j}$$$, the number of people living in the room $$$(i, j)$$$. Your task is to find the minimum total discomfort that can be achieved by arranging the windows properly.
The first line contains two integers $$$N$$$ and $$$M$$$: the dimensions of the grid.
Each of the following $$$N$$$ lines contains $$$M$$$ space-separated integers $$$p_{i,j}$$$.
Each of the following $$$N$$$ lines contains $$$M$$$ space-separated integers $$$w_{i,j}$$$.
Output one integer: the minimum total discomfort.
4 3 1 7 10 7 2 8 7 9 10 4 6 4 3 3 3 3 2 4 4 3 4 2 2 3
178
4 3 2 2 9 9 8 4 8 4 5 7 5 2 0 1 0 1 0 1 0 0 1 0 1 0
0
| Название |
|---|


