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 repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 2147483600

int dp[1010][1010][1<<4];

int main(){
  int h,w;
  cin>>h>>w;
  vector<string> str(h);
  rep(i,h) cin>>str[i];
  vector<vector<int>> vec(h+2,vector<int>(w+2,0));
  rep(i,h) rep(j,w) vec[i+1][j+1] = ((str[i][j]=='.')?(0):(str[i][j]-'0'));

  fill(dp[0][0], dp[1010][0], INF);

  dp[1][1][0] = 0;
  rep(i,h+1) rep(j,w+1) rep(mask, 1<<4) if(dp[i][j][mask]<INF){//dbg(mp(i,j));
    bool isu = mask%2;
    bool isl = (mask>>1)%2;
    bool isur = (mask>>2)%2;
    bool isld = (mask>>3)%2;
    int vu = (isu?0:vec[i-1][j]);
    int vl = (isl?0:vec[i][j-1]);
    int vr = vec[i][j+1];
    int vd = vec[i+1][j];

    if(i<h){ // down
      // ????????¨?????????
      int ncost = vr+vl+vd;
      int nmask = 1 + (isld<<1) + (1<<2);
      dp[i+1][j][nmask] = min(dp[i+1][j][nmask], dp[i][j][mask] + ncost);
      // ????????¨?????????
      ncost = vu+vl+vd;
      nmask = 1 + (isld<<1);
      dp[i+1][j][nmask] = min(dp[i+1][j][nmask], dp[i][j][mask] + ncost);
      // ?????????
      ncost = vu+vr+vd;
      nmask = 1 + (isld<<1) + (1<<2);
      dp[i+1][j][nmask] = min(dp[i+1][j][nmask], dp[i][j][mask] + ncost);
    }
    if(j<w){ // right
      // ??????
      int ncost = vr+vl+vd;
      int nmask = (isur) + (1<<1) +(1<<3);
      dp[i][j+1][nmask] = min(dp[i][j+1][nmask], dp[i][j][mask] + ncost);
      // ??????
      ncost = vr+vl+vu;
      nmask = (isur) + (1<<1);
      dp[i][j+1][nmask] = min(dp[i][j+1][nmask], dp[i][j][mask] + ncost);
      // ?????????
      ncost = vu+vr+vd;
      nmask = (isur) + (1<<1) +(1<<3);
      dp[i][j+1][nmask] = min(dp[i][j+1][nmask], dp[i][j][mask] + ncost);
    }
  }

  int ans = INF;
  rep(m,1<<4) ans = min(ans, dp[h][w][m]);
  cout << ans << endl;

  return 0;
}