#include <iostream>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
using namespace std;

#define INF 1145141919

int H, W;
int S[1010][1010];
int dp[1010][1010][64];

int G[4] = {1, 4, 8, 32};
int GX[4] = {0, -1, 1, 0};
int GY[4] = {-1, 0, 0, 1};
int DX[2] = {0, 1};
int DY[2] = {1, 0};

// [ ] [ ] [ ]
// [#] [#] [ ]
// [?] [#] [ ]

// [ ] [#] [?]
// [ ] [#] [#]
// [ ] [ ] [ ]

// [  ] [1 ] [2]
// [4 ] [_ ] [8]
// [16] [32] [ ]

int main() {
  cin >> H >> W;
  for (int i=1; i<=H; i++) {
    for (int j=1; j<=W; j++) {
      char c;
      cin >> c;
      if (c == '.') S[i][j] = 0;
      else          S[i][j] = (int)c-(int)'0';
    }
  }
  for (int sy=0; sy<=H+1; sy++) {
    for (int sx=0; sx<=W+1; sx++) {
      for (int s=0; s<64; s++) {
        dp[sy][sx][s] = INF;
      }
    }
  }

  dp[1][1][0] = 0;
  for (int sy=1; sy<=H; sy++) {
    for (int sx=1; sx<=W; sx++) {
      for (int d=0; d<2; d++) {
        int dx = sx + DX[d], dy = sy + DY[d];

        for (int s=0; s<64; s++) {
          int state;
          if (d == 0) state = 1 + 2*((s&8)!=0) + 4*((s&16) != 0); // (sy, sx) -> (sy+1, sx)
          else        state = 1*((s&2)!=0) + 4 + 16*((s&32) != 0); // (sy, sx) -> (sy, sx+1)
          if (dp[sy][sx][s] == INF) continue;

          for (int k=0; k<16; k++) {
            int nk[4] = {(k&1)!=0, (k&2)!=0, (k&4)!=0, (k&8)!=0};
            bool ok = true;
            for (int i=0; i<4; i++) {
              // already bought
              if (nk[i] && (state & G[i]) != 0) ok = false;
            }
            if (!ok) continue;
            int sc = 0;
            if ((s & (d == 0 ? 32 : 8)) == 0) sc = S[dy][dx];
            int ns = state;
            int ctr = 0, ctr2 = 0;
            for (int i=0; i<4; i++) {
              if ((state & G[i]) != 0) continue;
              if (S[dy+GY[i]][dx+GX[i]] > 0) ctr2++;
              if (nk[i]) {
                ns |= G[i];
                sc += S[dy+GY[i]][dx+GX[i]];
              }
              else {
                if (S[dy+GY[i]][dx+GX[i]] > 0) ctr++;
              }
            }
            if (ctr != 1 && ctr2 != 0) continue;
            if (((d == 0 ? 1 : 4) & ns) == 0) continue;
            dp[dy][dx][ns] = min(dp[dy][dx][ns], dp[sy][sx][s] + sc);
          }
        }
      }
    }
  }
  int ans = INF;
  for (int s=0; s<64; s++) {
    ans = min(ans, dp[H][W][s]);
  }
  cout << ans << "\n";
  return 0;
}