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