QOJ.ac

QOJ

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

#10255. Thin Ice

Statistics

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$

Discussions

About Discussions

The discussion section is only for posting: Editorials, General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues. Submitting multiple issues may cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.