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;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
const int MAX = 1010;
const int INF = MAX * MAX * 10;
int dp[2][4][4][MAX][MAX]={};
int ts[MAX][MAX]={};

signed main(){
  int h,w;
  cin>>h>>w;
  vector<string> vs(h);
  for(int i=0;i<h;i++) cin>>vs[i],vs[i]=".."+vs[i]+"..";
  vs.emplace(vs.begin(),string(w+4,'.'));
  vs.emplace(vs.begin(),string(w+4,'.'));
  vs.emplace_back(string(w+4,'.'));
  vs.emplace_back(string(w+4,'.'));
    
  for(int i=0;i<MAX;i++)
    for(int j=0;j<MAX;j++)
      for(int pp=0;pp<2;pp++)
        for(int pd=0;pd<4;pd++)
          for(int cd=0;cd<4;cd++)
            dp[pp][pd][cd][i][j]=INF;

  for(int pp=0;pp<2;pp++)
    for(int pd=0;pd<4;pd++)
      for(int cd=0;cd<4;cd++)
        dp[pp][pd][cd][2][2]=0;

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

  for(int i=2;i<=h+1;i++){
    for(int j=2;j<=w+1;j++){
      int cy=i,cx=j;
      for(int pp=0;pp<2;pp++){
        int py=cy-dy[pp],px=cx-dx[pp];
        for(int pd=0;pd<4;pd++){
          for(int cd=0;cd<4;cd++){
            cnt++;
            ts[py][px]=ts[cy][cx]=cnt;
            for(int k=0;k<4;k++){
              if(pd!=k) ts[py+dy[k]][px+dx[k]]=cnt;
              if(cd!=k) ts[cy+dy[k]][cx+dx[k]]=cnt;
            }
            int dist=dp[pp][pd][cd][cy][cx];
            for(int npp=0;npp<2;npp++){              
              int npd=cd,ncy=cy+dy[npp],ncx=cx+dx[npp];              
              if(ts[ncy][ncx]!=cnt) continue;
              int sum=0;
              int tmp[4]={};
              for(int k=0;k<4;k++){
                int ty=ncy+dy[k],tx=ncx+dx[k];
                if(ts[ty][tx]!=cnt&&isdigit(vs[ty][tx]))
                  tmp[k]=vs[ty][tx]-'0';
                sum+=tmp[k];                
              }
              for(int ncd=0;ncd<4;ncd++){
                int ndist=dist+sum-tmp[ncd];
                chmin(dp[npp][npd][ncd][ncy][ncx],ndist);                
              }
            }
          }
        }
      }      
    }    
  }
  
  int ans=INF;  
  for(int pp=0;pp<2;pp++)
    for(int pd=0;pd<4;pd++)
      for(int cd=0;cd<4;cd++)
        chmin(ans,dp[pp][pd][cd][h+1][w+1]);  
  cout<<ans<<endl;
  return 0;
}