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>
#define PB push_back
#define MP make_pair<int,int>
#define FI first
#define SE second
using namespace std;
typedef pair<int,int> pii;
static const int  INF = 1<<24;
static const int dx[] = {1,-1,0,0};
static const int dy[] = {0,0,1,-1};

int h,w;
int fld[1001][1001];
int dp[1001][1001][3][3][2][2];
string s[1001];

int dfs(int x,int y,int pbit,int ppbit,int pv,int ppv){
    if(dp[x][y][pbit][ppbit][pv][ppv]!=0)return dp[x][y][pbit][ppbit][pv][ppv];
    if(x<0||x>=w||y<0||y>=h)return INF;
    if(x==w-1&&y==h-1){
        return 0;
    }
    int res=INF;
    vector<pii> v;
    int p=0;
    if(pv==-1){
        p=fld[y][x];
        for(int i=0;i<4;++i){
            int nx=x+dx[i],ny=y+dy[i];
            if(nx<0||nx>=w||ny<0||ny>=h)continue;
            if(fld[ny][nx])v.PB(pii(dx[i],dy[i]));
        }
    }
    else if(pv==ppv||ppv==-1){
        if(pv&&pbit==2)p=fld[y][x];
        else if(pv==0&&pbit==1)p=fld[y][x];
        for(int i=0;i<4;++i){
            int nx=x+dx[i],ny=y+dy[i];
            if(nx<0||nx>=w||ny<0||ny>=h)continue;
            if(pv){
                if(dx[i]==0&&dy[i]==-1)continue;
                if(fld[ny][nx])v.PB(pii(dx[i],dy[i]));
            }
            else {
                if(dx[i]==-1&&dy[i]==0)continue;
                if(fld[ny][nx])v.PB(pii(dx[i],dy[i]));
            }
        }
    }
    else if(pv!=ppv){
        if(pv&&pbit==2)p=fld[y][x];
        else if(pv==0&&pbit==1)p=fld[y][x];
        for(int i=0;i<4;++i){
            int nx=x+dx[i],ny=y+dy[i];
            if(nx<0||nx>=w||ny<0||ny>=h)continue;
            if(pv){
                if(dx[i]==0&&dy[i]==-1)continue;
                if(dx[i]==-1&&dy[i]==0&&(ppbit==1||ppbit==0))continue;
                if(fld[ny][nx])v.PB(pii(dx[i],dy[i]));
            }
            else {
                if(dx[i]==-1&&dy[i]==0)continue;
                if(dx[i]==0&&dy[i]==-1&&(ppbit==2||ppbit==0))continue;
                if(fld[ny][nx])v.PB(pii(dx[i],dy[i]));
            }
        }
    }
    int sum=0;
    bool b=false;
    for(int i=0;i<v.size();++i){
        int nx=x+v[i].FI,ny=y+v[i].SE;
        sum+=fld[ny][nx];
    }
    if(v.size()==0)res=min(dfs(x+1,y,0,pbit,0,pv)+p,min(dfs(x,y+1,0,pbit,1,pv)+p,res));
    else{
        for(int i=0;i<v.size();++i){
            int nx=x+v[i].FI,ny=y+v[i].SE;
            int u=p+sum-fld[ny][nx];
            if((v[i].FI==-1&&v[i].SE==0)||(v[i].FI==0&&v[i].SE==-1)){
                res=min(dfs(x+1,y,0,pbit,0,pv)+u,min(dfs(x,y+1,0,pbit,1,pv)+u,res));
            }
            else if(v[i].FI==1&&v[i].SE==0){
                res=min(dfs(x+1,y,1,pbit,0,pv)+u,min(dfs(x,y+1,1,pbit,1,pv)+u,res));
            }
            else if(v[i].FI==0&&v[i].SE==1){
                res=min(dfs(x+1,y,2,pbit,0,pv)+u,min(dfs(x,y+1,2,pbit,1,pv)+u,res));
            }
        }
    }
    return dp[x][y][pbit][ppbit][pv][ppv]=res;
}

int main(){
    memset(dp,0,sizeof(dp));
    cin>>h>>w;
    for(int y=0;y<h;++y)cin>>s[y];
    for(int y=0;y<h;++y){
        for(int x=0;x<w;++x){
            if(s[y][x]=='.')fld[y][x]=0;
            else fld[y][x]=s[y][x]-'0';
        }
    }
    cout<<dfs(0,0,0,0,-1,-1)<<endl;
}