Polish version    English version  
  About Olympic -> Problems


 News
 About Olympic
About contest
Problems
I OI 1993/1994
II OI 1994/1995
III OI 1995/1996
IV OI 1996/1997
V OI 1997/1998
VI OI 1998/1999
VII OI 1999/2000
VIII OI 2000/2001
IX OI 2001/2002
X OI 2002/2003
XI OI 2003/2004
XII OI 2004/2005
XIII OI 2005/2006
XIV OI 2006/2007
XV OI 2007/2008
Problems archive
 History of OI
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN

Ploughing

Memory limit: 64 MB

Byteasar, the farmer, wants to plough his rectangular field . He can begin with ploughing a slice from any of the field's edges, then he can plough a slice from any unploughed field's edges, and so on, until the whole field is ploughed. After the ploughing of every successive slice, the yet-unploughed field has a rectangular shape. Each slice has a span of 1, and the length and width of the field are the integers and .

Unfortunately, Byteasar has only one puny and frail nag (horse) at his disposal for the ploughing. Once the nag starts to plough a slice, it won't stop until the slice is completely ploughed. However, if the slice is to much for the nag to bear, it will die of exhaustion, so Byteasar has to be careful. After every ploughed slice, the nag can rest and gather strength. The difficulty of certain parts of the field varies, but Byteasar is a good farmer and knows his field well, hence he knows every part's ploughing-difficulty.

Let us divide the field into unitary squares -- these are called tiles in short. We identify them by their coordinates , for and . Each tile has its ploughing-difficulty - a non-negative integer. Let denotes the difficulty of the tile which coordinates are . For every slice, the sum of ploughing-difficulties of the tiles forming it up cannot exceed a certain constant - lest the nag dies.

A difficult task awaits Byteasar: before ploughing each subsequent slice he has to decide which edge of the field he'll plough, so that the nag won't die. On the other hand, he'd like to plough as few slices as possible.

Task

Write a programme that:

  • reads the numbers , and , from the standard input, as well as the ploughing-difficulty coefficients,
  • determines the best way to plough Byteasar's field,
  • writes the result to the standard output.

Input

There are three positive integers in the first line of the standard input: , and , separated by single spaces, , , . In the following lines there are the ploughing-difficulty coefficients. The line no. contains the coefficients , separated by single spaces, .

Output

Your programme should write one integer to the standard ouput: the minimum number of slices required to plough the field while satisfying the given conditions. Since we care for animals, we guarantee that the field can be ploughed according to the above rules. But remember, saving the nag is up to you!

For the following input data:
12 6 4
6 0 4 8 0 5
0 4 5 4 6 0
0 5 6 5 6 0
5 4 0 0 5 4
the correst answer is:
8

The illustration above shows the optimal ploughing of the field from the example.




Print friendly version