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;

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
typedef vector<int>vint;
typedef pair<int,int>pint;

const int dy[]={0,-1,0,1};
const int dx[]={-1,0,1,0};

int H,W;
int a[1000][1000];

int memo[1000][1000][1<<4];

int dfs(int y,int x,int s){
    if(~memo[y][x][s])return memo[y][x][s];
    if(y+1==H&&x+1==W)return 0;



    int ret=1145141919;

    int lis[4]={0},sum=0;
    rep(i,4){
        int ny=y+dy[i],nx=x+dx[i];
        if(ny<0||ny>=H||nx<0||nx>=W||!a[ny][nx])continue;
        if(i==0&&(s>>1&1))continue;
        if(i==1&&(s>>2&1))continue;
        lis[i]=a[ny][nx];
        sum+=lis[i];
    }

    if(y+1!=H){
        if(sum==0){
            ret=min(ret,dfs(y+1,x,(s&1)*2+4));
        }
        else if(sum==lis[3]){
            ret=min(ret,dfs(y+1,x,(s&1)*2+4)+sum);
        }
        else{
            if(lis[0]){
                ret=min(ret,dfs(y+1,x,(s&1)*2+12)+sum-lis[0]);
            }
            if(lis[1]){
                ret=min(ret,dfs(y+1,x,(s&1)*2+12)+sum-lis[1]);
            }
            if(lis[2]){
                ret=min(ret,dfs(y+1,x,(s&1)*2+4)+sum-lis[2]);
            }
        }
    }
    if(x+1!=W){
        if(sum==0){
            ret=min(ret,dfs(y,x+1,(s>>3&1)*4+2));
        }
        else if(sum==lis[2]){
            ret=min(ret,dfs(y,x+1,(s>>3&1)*4+2)+sum);
        }
        else{
            if(lis[0]){
                ret=min(ret,dfs(y,x+1,(s>>3&1)*4+3)+sum-lis[0]);
            }
            if(lis[1]){
                ret=min(ret,dfs(y,x+1,(s>>3&1)*4+3)+sum-lis[1]);
            }
            if(lis[3]){
                ret=min(ret,dfs(y,x+1,(s>>3&1)*4+2)+sum-lis[3]);
            }
        }
    }
    return memo[y][x][s]=ret;
}

signed main(){

    scanf("%d%d",&H,&W);
    rep(i,H)rep(j,W){
        char c;scanf(" %c",&c);
        if(isdigit(c))a[i][j]=c-'0';
    }

    memset(memo,-1,sizeof(memo));
    printf("%d\n",dfs(0,0,0));

    return 0;
}