This page is still under construction.

Parts of this page are still being built. What you see may change.

Deforestation

Time limit2sMemory limit512 MB

Summary
Cut trees in a grid so the top-left and bottom-right cells become connected, minimizing the total walking time to cut each tree and haul it back to the mill.
Level

Hard8 of 10

Topics
Graph, Shortest path, Dynamic programming, Greedy
Solved
No attempts yet

Problem

The JOI Kingdom has a wide forest. The forest is a rectangle, divided into a grid of HH cells from north to south and WW cells from west to east. The cell that is ii-th from the north and jj-th from the west (1≤i≤H1 \le i \le H, 1≤j≤W1 \le j \le W) has Ai,jA_{i,j} trees growing in it. The cell at the northwest corner has a lumber mill, so no tree grows there. That is, A1,1=0A_{1,1} = 0.

A person can enter a cell that has no tree. A person can move to a cell adjacent to the north, south, east, or west of the current cell if that cell has no tree. Nobody can go outside the forest. As a public works project of the kingdom, JOI wants to cut trees so that a person can travel between the northwest corner cell and the southeast corner cell in both directions.

The cutting goes as follows. At the start JOI is at the northwest corner cell, where the lumber mill is. JOI moves in 1 minute to a cell adjacent to the north, south, east, or west of the current cell if that cell has no tree. JOI also cuts one tree in 1 minute from a cell adjacent to the north, south, east, or west of the current cell. Every time JOI cuts one tree, JOI has to carry that tree to the lumber mill at the northwest corner cell. JOI moves at the same speed while carrying a tree. JOI cannot cut another tree while carrying one.

Find the minimum time needed to cut trees so that the condition holds. The cutting time is the time until the last tree that was cut is carried to the lumber mill.

Input

The input is given from standard input in the following format.

H W
A_{1,1} ... A_{1,W}
...
A_{H,1} ... A_{H,W}

Output

Print the minimum time needed to cut trees so that the condition holds, on one line.

Constraints

  • 1≤H≤301 \le H \le 30
  • 1≤W≤301 \le W \le 30
  • (H,W)≠(1,1)(H, W) \ne (1, 1)
  • 0≤Ai,j≤100000 \le A_{i,j} \le 10000 (1≤i≤H1 \le i \le H, 1≤j≤W1 \le j \le W)
  • A1,1=0A_{1,1} = 0

Examples3

  1. Example 1

    Input
    2 3
    0 1 2
    3 4 5
    
    Expected output
    32
    
  2. Example 2

    Input
    2 5
    0 5 0 0 0
    0 0 0 9 1
    
    Expected output
    13
    
  3. Example 3

    Input
    2 5
    0 2 0 0 0
    0 0 0 9 1
    
    Expected output
    11