CSES - NOI 2024 - Thin Ice
  • Time limit: 1.00 s
  • Memory limit: 512 MB

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?


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.


Print the largest number of coins that Uolevi can gather.



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




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