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:
class in{struct myIterator{int it;const bool rev;explicit constexpr myIterator(int it_, bool rev=false):it(it_),rev(rev){}int operator*(){return it;}bool operator!=(myIterator& r){return it!=r.it;}void operator++(){rev?--it:++it;}};const myIterator i,n;public:explicit constexpr in(int n):i(0),n(n){}explicit constexpr in(int i,int n):i(i,n<i),n(n){}const myIterator& begin(){return i;}const myIterator& end(){return n;}};

#include <bits/stdc++.h>
using namespace std;

#ifdef COMMENT
.XR... R := ??????????????´?????????????????????????§????L?????????????????´?????????
X@.... ????±??????\????????????????????????????¶?????????????R/L????¶????????????????????????°??????
L.....
......
#endif

int h, w;
int dp[1000][1000][8];
vector<string> fld;
const int INF = 1e9;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
inline bool range_out(int y, int x) { return y >= h || x >= w || y < 0 || x < 0;}
int dfs(int y, int x, int code) {
    if(y == h - 1 && x == w - 1) return 0;
    int& res = dp[y][x][code];
    if(res != -1) return res;
    res = INF;
    
    bool from = code & 1, other = code & 2;
    vector<int> cand;
    int sum = 0, right = 0, down = 0;
    for(int i : in(4)) {
        int ax = x + dx[i], ay = y + dy[i];
        if(range_out(ay, ax)) continue;
        if(fld[ay][ax] == '.') continue;
        if(i == 0) if(from || other) continue;
        if(i == 1) if(!from || other) continue;
        cand.emplace_back(i);
        sum += fld[ay][ax] - '0';
        if(i == 2) right = fld[ay][ax] - '0';
        if(i == 3) down = fld[ay][ax] - '0';
    }
    // east
    if(x < w - 1) {
        int nxt_code = 1;
        if(!from && (code & 4)) nxt_code |= 2;
        if(cand.empty()) res = min(res, dfs(y, x + 1, nxt_code));
        for(int i : cand) {
            int plus = sum - (fld[y + dy[i]][x + dx[i]] - '0');
            if(i != 2 && right) plus -= right;
            int tcode = i != 3 && down ? 4 : 0;
            res = min(res, dfs(y, x + 1, nxt_code | tcode) + plus);
        }
    }
    // south
    if(y < h - 1) {
        int nxt_code = 0;
        if(from && (code & 4)) nxt_code |= 2;
        if(cand.empty()) res = min(res, dfs(y + 1, x, nxt_code));
        for(int i : cand) {
            int plus = sum - (fld[y + dy[i]][x + dx[i]] - '0');
            if(i != 3 && down) plus -= down;
            int tcode = i != 2 && right ? 4 : 0;
            res = min(res, dfs(y + 1, x, nxt_code | tcode) + plus);
        }
    }
    if(fld[y][x] != '.') res += fld[y][x] - '0';
    return res;
}
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> h >> w;
    fld.resize(h);
    for(auto& s: fld) cin >> s;
    memset(dp, -1, sizeof(dp));
    cout << dfs(0, 0, 0) << endl;
//    for(int y : in(h)) for(int x : in(w)) for(int code : in(8))
//      cout << y << ' ' << x << ' ' << code << ' ' << dp[y][x][code] << endl;
    return 0;
}