QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#10255. Thin Ice

統計

Uolevi is at a frozen lake in the shape of an $n \times m$ grid, with one coin on each square. Each square has a durability: the maximum number of coins the ice on the square can withstand.

In one step, Uolevi can move one square up, down, left or right, but not outside the lake. If there is a coin on the square Uolevi is currently in, he can pick it up.

When Uolevi moves to a square, the number of coins on the square must never exceed the durability of the square. This includes the coins Uolevi is carrying, and the one on the ice if it has not yet been picked up. Uolevi's own weight is negligible.

Uolevi wants to start and end a trip on some edge squares of the lake. What is the largest number of coins he can gather during the trip?

Input

The first line of input contains the integers $n$ and $m$: the height and width of the lake.

Then follow $n$ lines with $m$ integers each: the durability $d$ of the ice on each square.

Output

Print the largest number of coins that Uolevi can gather.

Example

Input:

3 4
1 1 1 1
1 3 6 1
3 4 5 1

Output:

5

Explanation:

Uolevi can start at the top left square and move as follows:

down $\rightarrow$ pick coin $\rightarrow$ right $\rightarrow$ pick coin $\rightarrow$ down $\rightarrow$ left $\rightarrow$ pick coin $\rightarrow$ right $\rightarrow$ pick coin $\rightarrow$ right $\rightarrow$ pick coin

Note that Uolevi cannot collect six coins because he would not be able to get back to an edge square after this.

Subtask 1 (17 points)

  • $1 \le nm \le 16$
  • $1 \le d \le 16$

Subtask 2 (12 points)

  • $1 \le nm \le 2 \cdot 10^5$
  • $1 \le d \le 5$

Subtask 3 (11 points)

  • $n = 1$, $1 \le m \le 100$
  • $1 \le d \le 100$

Subtask 4 (19 points)

  • $n = 1$, $1 \le m \le 2 \cdot 10^5$
  • $1 \le d \le 2 \cdot 10^5$

Subtask 5 (14 points)

  • $1 \le nm \le 1000$
  • $1 \le d \le 1000$

Subtask 6 (27 points)

  • $1 \le nm \le 2 \cdot 10^5$
  • $1 \le d \le 2 \cdot 10^5$
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.