#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;

  
}