Problem: problem

The city of IOI, where JOI lives, has a rectangular shape with H sections in the north-south direction and W sections in the east-west direction, and is divided into H x W sections. The i-th section from the north and the j-th section from the west are represented as (i, j). A large-scale festival is currently being held to commemorate the international programming contest held in IOI City. There are food stalls in some areas, each selling different types of sweets. There are no food stalls in plots (1, 1), plots (H, W) and the plots adjacent to them north, south, east and west.

JOI moves from compartment (1, 1) to compartment (H, W). To shorten the travel time, JOI travels only to the east or south. JOI likes sweets, so he takes the following actions in order each time he enters a section.

1. If there is a stall selling sweets that you have not bought yet in the current section, buy sweets at that stall.
2. If there are food stalls selling sweets that you have not bought yet in the area adjacent to the north, south, east, and west of the current area, call a salesperson from all the stalls except one of those stalls. Buy sweets.



JOI never buys the same kind of sweets more than once.

Given the size of the IOI city, the location of the stalls, and the price of the sweets at each stall, the sweets that JOI buys while moving from parcel (1, 1) to parcel (H, W). Find the minimum value of the total amount of.





Example

Input

5 5
..483
.59.9
3.866
79...
4.8..


Output

20

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;
inline void chmin(int &a, int b) { a = min(a, b); }

int H, W;
int dp[1005][1005][1 << 4];
int data[1005][1005] = {};

int main()
{
    fill_n(**dp, 1005 * 1005 * 16, INF);
    cin >> H >> W;
    for(int i = 1; i <= H; i++) {
        for(int j = 1; j <= W; j++) {
            char ch;
            cin >> ch;
            if(ch != '.') data[i][j] = ch - '0';
        }
    }

    dp[1][1][0] = 0; //0 = とってある 1 = とってない
    for(int i = 1; i <= H; i++) {
        for(int j = 1; j <= W; j++) {
            for(int k = 0; k < (1 << 4); k++) {
                int val; 
                if(dp[i][j][k] == INF) continue;
                //右
                if(j < W) {
                    int shift = (((k >> 2) & 1) << 3) | (1 << 1);
                    int a = data[i - 1][j + 1] * ((k >> 1) & 1), b = data[i][j + 2], c = data[i + 1][j + 1];
                    val = (k & 1) * data[i][j + 1] + dp[i][j][k] + a + b + c;
                    chmin(dp[i][j + 1][shift], val - a);
                    chmin(dp[i][j + 1][shift | 1], val - b);     //下
                    chmin(dp[i][j + 1][shift | (1 << 2)], val - c);  //右
                }
                //下
                if(i < H) {
                    int shift = ((k & 1) << 1) | (1 << 3);
                    int a = data[i + 1][j - 1] * ((k >> 3) & 1), b = data[i + 2][j], c = data[i + 1][j + 1];
                    val = ((k >> 2) & 1) * data[i + 1][j] + dp[i][j][k] + a + b + c;
                    chmin(dp[i + 1][j][shift], val - a);
                    chmin(dp[i + 1][j][shift | (1 << 2)], val - b);
                    chmin(dp[i + 1][j][shift | 1], val - c);
                }
            }
        }
    }
    int minv = INF;
    for(int i = 0; i < (1 << 4); i++) {
        chmin(minv, dp[H][W][i]);
    }
    cout << minv << endl;
    return (0);
}