QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100
[0]

# 10255. Thin Ice

Statistics

Uolevi is at a frozen lake in the shape of an n×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 pick coin right pick coin down left pick coin right pick coin right 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)

  • 1nm16
  • 1d16

Subtask 2 (12 points)

  • 1nm2105
  • 1d5

Subtask 3 (11 points)

  • n=1, 1m100
  • 1d100

Subtask 4 (19 points)

  • n=1, 1m2105
  • 1d2105

Subtask 5 (14 points)

  • 1nm1000
  • 1d1000

Subtask 6 (27 points)

  • 1nm2105
  • 1d2105