Plovdiv's famous master chocolatier Bonny needs to cut a slab of chocolate with raisins. The chocolate is a rectangular block of identical square pieces. The pieces are aligned with the edges of the chocolate, and they are arranged in
Initially, the chocolate is one single, monolithic block. Bonny needs to cut it into smaller and smaller blocks until finally she has cut the chocolate down to its
Bonny wants to pay Peter as little as possible. She knows how many raisins there are on each of the
Task
Write a program that, given the number of raisins on each of the individual pieces, determines the minimum number of raisins that Bonny will have to pay Sly Peter.
Constraints
|
The number of pieces on each side of the chocolate |
|
The number of raisins on the piece in the |
Input Specification
Your program must read from standard input the following data:
- The first line contains the integers
and , separated by a single space. - The next
lines describe how many raisins there are on each piece of the chocolate. The th of these lines describes the th row of the chocolate. Each such line contains integers separated by single spaces. The integers describe the pieces on the corresponding row in order from left to right. The th integer on the th line (among these lines) tells you how many raisins are on the piece in the th row and the th column.
Output Specification
Your program must write to standard output a single line containing a single integer: the minimum possible number of raisins that Bonny would have to pay Sly Peter.
Grading
For a number of tests, worth a total of
Sample Input
2 3
2 7 5
1 9 5
Sample Output
77
One possible way (out of many) to achieve a cost of
The first cut that Bonny asks Peter to make separates the third column from the rest of the chocolate. Bonny needs to pay Peter
Then Bonny gives Peter the smaller of the two blocks: the one that has two pieces with
After this, Bonny gives Peter the largest remaining block: the one having pieces with
Following this, Bonny gives Peter the top-left block, paying
The total cost to Bonny is
Comments