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;
using ll = long long;
template<class T,class U> using P = pair<T,U>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;

int dp[1002][1002][2][2][2][2];

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H,W;
    cin >> H >> W;
    vvec<int> F(H+2,vec<int>(W+2,0));
    for(int i=1;i<=H;i++) for(int j=1;j<=W;j++){
        char c;
        cin >> c;
        if(c!='.') F[i][j] = c-'0';
    }
    const int inf = 1e9;
    for(int i=0;i<=H;i++) for(int j=0;j<=W;j++){
        for(int a=0;a<2;a++) for(int b=0;b<2;b++) for(int c=0;c<2;c++) for(int d=0;d<2;d++) dp[i][j][a][b][c][d] = inf;
    }
    for(int a=0;a<2;a++) for(int b=0;b<2;b++) for(int c=0;c<2;c++) for(int d=0;d<2;d++) dp[1][1][a][b][c][d] = 0;
    for(int i=1;i<=H;i++) for(int j=1;j<=W;j++){
        for(int a=0;a<2;a++) for(int b=0;b<2;b++) for(int c=0;c<2;c++) for(int d=0;d<2;d++){
            //右に
            if(j<W){
                int p = (a? F[i][j+1]:0);
                //上を取らない
                dp[i][j+1][0][1][0][c] = min(dp[i][j+1][0][1][0][c],dp[i][j][a][b][c][d]+p+F[i][j+2]+F[i+1][j+1]);
                //右を取らない
                dp[i][j+1][1][1][0][c] = min(dp[i][j+1][1][1][0][c],dp[i][j][a][b][c][d]+p+(b? F[i-1][j+1]:0)+F[i+1][j+1]);
                //下を取らない
                dp[i][j+1][0][1][1][c] = min(dp[i][j+1][0][1][1][c],dp[i][j][a][b][c][d]+p+(b? F[i-1][j+1]:0)+F[i][j+2]);
            }
            if(i<H){
                int p = (c? F[i+1][j]:0);
                //左を取らない
                dp[i+1][j][0][a][0][1] = min(dp[i+1][j][0][a][0][1],dp[i][j][a][b][c][d]+p+F[i+2][j]+F[i+1][j+1]);
                //右を取らない
                dp[i+1][j][1][a][0][1] = min(dp[i+1][j][1][a][0][1],dp[i][j][a][b][c][d]+p+(d? F[i+1][j-1]:0)+F[i+2][j]);
                //下を取らない
                dp[i+1][j][0][a][1][1] = min(dp[i+1][j][0][a][1][1],dp[i][j][a][b][c][d]+p+(d? F[i+1][j-1]:0)+F[i+1][j+1]);
            }
        }
    }
    int ans = inf;
    for(int a=0;a<2;a++) for(int b=0;b<2;b++) for(int c=0;c<2;c++) for(int d=0;d<2;d++) ans = min(ans,dp[H][W][a][b][c][d]);
    cout << ans << endl;
}