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;
int dy[]={-1,0,1,0};
int dx[]={0,1,0,-1};

int H,W,INF=1e9;
char s[1005];
int t[1005][1005];
int dp[1005][1005][16];

int main(){
  scanf("%d %d",&H,&W);
  for(int i=0;i<H;i++){
    scanf("%s",s);
    for(int j=0;j<W;j++){
      if(s[j]!='.')t[i+1][j+1]=s[j]-'0';
    }
  }
  int ans=INF;
  fill((int*)dp,(int*)dp[1005],INF);
  dp[1][1][0]=0;
  for(int i=1;i<=H;i++){
    for(int j=1;j<=W;j++){
      for(int bit=0;bit<(1<<4);bit++){
        if(dp[i][j][bit]==INF)continue;
        int cost=dp[i][j][bit];
        int a=bit>>0&1,b=bit>>1&1,c=bit>>2&1,d=bit>>3&1;
        int nbit,ncost;
        
        nbit=b+2*0+4*1+8*1;
        ncost=cost+t[i][j]+t[i+1][j]+t[i][j-1]*(1-c);
        dp[i][j+1][nbit]=min(dp[i][j+1][nbit],ncost);
        
        nbit=1*b+2*0+4*1+8*0;
        ncost=cost+t[i][j]+t[i-1][j]*(1-a)+t[i][j-1]*(1-c);
        dp[i][j+1][nbit]=min(dp[i][j+1][nbit],ncost);
        
        nbit=1*b+2*0+4*1+8*1;
        ncost=cost+t[i][j]+t[i-1][j]*(1-a)+t[i+1][j];
        dp[i][j+1][nbit]=min(dp[i][j+1][nbit],ncost);
        
        nbit=1+2*1+4*d+8*0;
        ncost=cost+t[i][j]+t[i][j+1]+t[i][j-1]*(1-c);
        dp[i+1][j][nbit]=min(dp[i+1][j][nbit],ncost);
        
        nbit=1+2*0+4*d+8*0;
        ncost=cost+t[i][j]+t[i-1][j]*(1-a)+t[i][j-1]*(1-c);
        dp[i+1][j][nbit]=min(dp[i+1][j][nbit],ncost);

        nbit=1+2*1+4*d+8*0;
        ncost=cost+t[i][j]+t[i-1][j]*(1-a)+t[i][j+1];
        dp[i+1][j][nbit]=min(dp[i+1][j][nbit],ncost);

        if(i==H&&j==W)ans=min(ans,ncost);
      }
    }
  }
  
  printf("%d\n",ans);
  return 0;
}