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 all(vec) vec.begin(),vec.end()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const ll MOD=1000000007;
const ll INF=1000000010;
const ll LINF=1000000000000000010;
int dx[5]={0,1,0,-1,0};
int dy[5]={1,0,-1,0,0};
char c[1010][1010];
int dp[1010][1010][1<<7];//南、東、北、西、今いる場所、南西、北東
int h,w;
int change(int bit,int dir){
    int res=0;
    if(dir){
        if(bit&(1<<0))res|=(1<<4);//南から今いる場所
        if(bit&(1<<4))res|=(1<<2);//今いる場所から北
        if(bit&(1<<5))res|=(1<<3);//南西から西
        if(bit&(1<<1))res|=(1<<6);//東から北東
    }else{
        if(bit&(1<<0))res|=(1<<5);//南から南西
        if(bit&(1<<1))res|=(1<<4);//東から今いる場所
        if(bit&(1<<4))res|=(1<<3);//今いる場所から西
        if(bit&(1<<6))res|=(1<<2);//北東から北
    }
    return res;
}
int dfs(int y,int x,int bit){
    if(dp[y][x][bit]!=-1)return dp[y][x][bit];
    if(y==h-1&&x==w-1)return 0;
    int res=INF;
    for(int j=0;j<4;j++){
        int s=0;
        int bit2=bit;
        for(int i=0;i<5;i++){
            if(i==j)continue;
            if(bit&(1<<i))continue;
            int ny=y+dy[i];
            int nx=x+dx[i];
            if(ny<0||nx<0||ny>=h||nx>=w)continue;
            if(c[ny][nx]!='.'){
                s+=c[ny][nx]-'0';
                bit2|=(1<<i);
            }
        }
        int a=INF;
        if(y<h-1){
            int nbit=change(bit2,1);
            a=dfs(y+1,x,nbit);
        }
        if(x<w-1){
            int nbit=change(bit2,0);
            a=min(a,dfs(y,x+1,nbit));
        }
        s+=a;
        res=min(res,s);
    }
    return dp[y][x][bit]=res;
}
int main(){
    cin>>h>>w;
    for(int i=0;i<h;i++){
        cin>>c[i];
    }
    memset(dp,-1,sizeof(dp));
    cout<<dfs(0,0,0)<<endl;
}