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 <fstream>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string>
#include <tuple>
#include <vector>
#include <map>
#include <unordered_map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <cassert>
using namespace std;
#define LL long long
#define MP(a, b) make_pair(a, b)
#define POWER9 1000000000
#define MOD POWER9+7
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define LL_MIN (LL)-9223372036854775807
#define LL_MAX (LL)9223372036854775807
#define PI 3.14159265359

int H,W;
int mp[1000][1000];
int cost[1000][1000][32];

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> H >> W;
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            char c;
            cin >> c;
            if(c == '.') mp[i][j] = 0;
            else mp[i][j] = c - '0';
            for(int k=0; k<32; k++) cost[i][j][k] = INT_MAX;
        }
    }
    cost[0][0][0] = 0;

    priority_queue<pair<int,pair<pair<int,int>,int> > > que;
    que.push(MP(INT_MAX, MP(MP(0,0), 0)));
    while(!que.empty()){
        int c = INT_MAX-que.top().first;
        int y = que.top().second.first.first;
        int x = que.top().second.first.second;
        int s = que.top().second.second;
        que.pop();
        if(cost[y][x][s] < c) continue;
        if(y==H-1 && x==W-1){
            cout << c << endl;
            return 0;
        }
        int d[5];
        for(int i=0; i<5; i++){
            d[i] = (s>>(4-i)) % 2;
        }

        int nc = 0;
        if(d[0] == 0) nc += mp[y][x];
        if(d[2] == 0 && y-1 >= 0) nc += mp[y-1][x];
        if(d[3] == 0 && x-1 >= 0) nc += mp[y][x-1];
        if(y+1 < H) nc += mp[y+1][x];
        if(x+1 < W) nc += mp[y][x+1];

        int tmpY = (d[4]==1 ? 2 : 0);
        int tmpX = (d[1]==1 ? 4 : 0);
        if(d[2] == 0 && y-1 >= 0){
            nc -= mp[y-1][x];
            if(y+1 < H && cost[y+1][x][16+8+4+tmpY] > c+nc){
                cost[y+1][x][16+8+4+tmpY] = c+nc;
                que.push(MP(INT_MAX-c-nc, MP(MP(y+1, x), 16+8+4+tmpY)));
            }
            if(x+1 < W && cost[y][x+1][16+2+1+tmpX] > c+nc){
                cost[y][x+1][16+2+1+tmpX] = c+nc;
                que.push(MP(INT_MAX-c-nc, MP(MP(y, x+1), 16+2+1+tmpX)));
            }
            nc += mp[y-1][x];
        }
        if(d[3] == 0 && x-1 >= 0){
            nc -= mp[y][x-1];
            if(y+1 < H && cost[y+1][x][16+8+4+tmpY] > c+nc){
                cost[y+1][x][16+8+4+tmpY] = c+nc;
                que.push(MP(INT_MAX-c-nc, MP(MP(y+1, x), 16+8+4+tmpY)));
            }
            if(x+1 < W && cost[y][x+1][16+2+1+tmpX] > c+nc){
                cost[y][x+1][16+2+1+tmpX] = c+nc;
                que.push(MP(INT_MAX-c-nc, MP(MP(y, x+1), 16+2+1+tmpX)));
            }
            nc += mp[y][x-1];
        }
        if(y+1 < H){
            nc -= mp[y+1][x];
            if(y+1 < H && cost[y+1][x][8+4+tmpY] > c+nc){
                cost[y+1][x][8+4+tmpY] = c+nc;
                que.push(MP(INT_MAX-c-nc, MP(MP(y+1, x), 8+4+tmpY)));
            }
            if(x+1 < W && cost[y][x+1][16+2+tmpX] > c+nc){
                cost[y][x+1][16+2+tmpX] = c+nc;
                que.push(MP(INT_MAX-c-nc, MP(MP(y, x+1), 16+2+tmpX)));
            }
            nc += mp[y+1][x];
        }
        if(x+1 < W){
            nc -= mp[y][x+1];
            if(y+1 < H && cost[y+1][x][16+4+tmpY] > c+nc){
                cost[y+1][x][16+4+tmpY] = c+nc;
                que.push(MP(INT_MAX-c-nc, MP(MP(y+1, x), 16+4+tmpY)));
            }
            if(x+1 < W && cost[y][x+1][2+1+tmpX] > c+nc){
                cost[y][x+1][2+1+tmpX] = c+nc;
                que.push(MP(INT_MAX-c-nc, MP(MP(y, x+1), 2+1+tmpX)));
            }
            nc += mp[y][x+1];
        }
    }

    return 0;
}