thinice (NOINordic contest)
Time limit: 1 seconds | Memory limit: 512 MB | Task type: batch
Algorithms: network flow, grid traversal, graph theory, binary search

# NOI 2024 Thin Ice

[TASK](https://cses.fi/495/task/C/) | [SUBMIT](https://cses.fi/495/submit/C/) | [RESULTS](https://cses.fi/495/view/C/)

**Time limit:** 1.00 s **Memory limit:** 512MB

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
```

Start: 2024-03-06 15:00:00
End: 2024-03-06 20:00:00
Contest has ended

## Tasks

*   **A** [Anime Shops](https://cses.fi/495/task/A)
*   **B** [Chair Game](https://cses.fi/495/task/B)
*   **C** [Thin Ice](https://cses.fi/495/task/C)

## Your submissions

## 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$

Starter code:

Grader code:

Setup script:

Evaluation script: