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 H,W;
int dp[1001][1001][4][4][4];
char fie[1001][1002];
bool F[1001][1001];

int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int db[]={2,1,3,3};

bool posc(int y,int x){
  return x < 0 || y < 0 || x >= W || y >= H;
}

void ume(int y,int x,int f,bool v){
  if( posc(y,x) ) return;
  F[y][x] = v;
  if( f & 1 ) F[y][x+1] = v;
  if( f >>1 ) F[y+1][x] = v;
}

int solve(int y,int x,int st,int f1,int f2){
  if( dp[y][x][st][f1][f2] != -1 ) return dp[y][x][st][f1][f2];
  int nt1 = st >> 1;
  int nt2 = st & 1;
  ume( y - dy[nt1], x - dx[nt1], f1, true );
  ume( y - dy[nt1] - dy[nt2], x - dx[nt1] - dx[nt2], f2, true );

  int sum = 0;
  int dc[4];
  if( !F[y][x] && fie[y][x] != '.' ) sum+=fie[y][x]-'0';
  for(int i=0;i<4;i++){
    int ny = y+dy[i], nx = x + dx[i];
    dc[i] = 0;
    if( posc(ny,nx) ) continue;
    if( !F[ny][nx] && fie[ny][nx] != '.' ) {
      sum += fie[ny][nx]-'0';
      dc[i] = fie[ny][nx]-'0';
    }
  }
 
  ume( y - dy[nt1], x - dx[nt1], f1, false );
  ume( y - dy[nt1] - dy[nt2], x - dx[nt1] - dx[nt2], f2, false ); 

  if( H-1 == y && W-1 == x ){
    int ret = sum;
    for(int i=0;i<4;i++){
      ret = min( ret,sum-dc[i] );    
    }
    return ret;
  }

  int ret = INT_MAX;
  for(int i=0;i<4;i++){
    for(int j=0;j<2;j++){      
      int ny = y+dy[j], nx = x + dx[j];
      if( posc(ny,nx) ) continue;
      int nst = st>>1;
      nst |= (j<<1);
      // cout << y << "," << x << " -> " << ny << "," << nx <<"  = " << sum-dc[i] << endl;
      ret = min( ret,solve( ny,nx, nst, db[i], f1 )+sum-dc[i] );
    }
  }
  return dp[y][x][st][f1][f2] = ret;
}

int main(){
  scanf("%d%d",&H,&W);
  for(int i=0;i<H;i++)
    scanf("%s",fie[i]);
  memset(dp,-1,sizeof(dp));

  cout << solve(0,0,0,0,0) << endl;

  
}