#P2958. [USACO09OCT] Papaya Jungle G

[USACO09OCT] Papaya Jungle G

Description

Bessie has accidentally wandered out of Farmer John’s field and into a neighboring farmer’s land. She picks up a papaya—papayas are a rare delicacy for cows. Like typical fields in Wisconsin, this papaya grove is partitioned into a grid of RR rows and CC columns (1R,C401 \le R, C \le 40). Bessie can move from one cell to an adjacent cell along a straight line parallel to the X-axis or Y-axis. She finds herself starting in the papaya grove at (1,1)(1, 1), the first row and first column, leisurely chewing on a papaya.

Bessie always uses her trusty binoculars to count the number of papayas hanging in each adjacent cell. Then she wanders to the adjacent cell that has the most papayas that have not yet been eaten (this cell is guaranteed to be unique).

Following this movement rule, Bessie will eventually stop at (R,C)(R, C) and eat the papayas there.

Given the size of the papaya grove and the number of papayas in each cell FijF_{ij} (1Fij1001 \le F_{ij} \le 100), determine the total number of papayas Bessie eats.

Input Format

Line 1: two integers RR and CC separated by a space.

The next RR lines: the (i+1)(i + 1)-th line describes row ii of the grove with CC space-separated integers Fi1,Fi2,,FiCF_{i1}, F_{i2}, \dots, F_{iC}.

Output Format

Line 1: a single integer, the total number of papayas Bessie eats by the time she finishes at the barn in the bottom-right corner at coordinates (R,C)(R, C).

3 4 
3 3 4 5 
4 5 3 2 
1 7 4 2 

39 

Hint

Bessie eats the papayas in the following order:

顺序

Translated by ChatGPT 5