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:
//Food Stalls
#include <iostream>
#include <climits>
#include <vector>

using namespace std;
static const int MAX = 1000;

int H, W;
int A[MAX + 2][MAX + 2], min_cost[MAX + 1][MAX + 1][16];
//(i - 1, j + 1), (i, j + 1), (i + 1. j), (i + 1, j - 1)

enum {
    SOUTHWEST = 0,
    SOUTH = 1,
    EAST = 2,
    NORTHEAST = 3,
    NORTH = 4,
    WEST = 5
};

int main() {
    cin >> H >> W;

    for(int i = 0; i < H + 2; i++) {
        A[i][0] = 0;
        A[i][W + 1] = 0;
    }
    for(int j = 0; j < W + 2; j++) {
        A[0][j] = 0;
        A[H + 1][j] = 0;
    }

    char c;
    for(int i = 1; i < H + 1; i++) {
        for(int j = 1; j < W + 1; j++) {
            cin >> c;
            if(c == '.') A[i][j] = 0;
            else A[i][j] = c - '0';
        }
    }

    for(int i = 1; i < H + 1; i++) {
        for(int j = 1; j < W + 1; j++) {
            for(int k = 0; k < 16; k++) min_cost[i][j][k] = INT_MAX;
        }
    }
    min_cost[1][1][0] = 0;

    for(int i = 1; i < H + 1; i++) {
        for(int j = 1; j < W + 1; j++) {
            for(int k = 0; k < 16; k++) {
                if(min_cost[i][j][k] == INT_MAX) continue;
                int next_cost, next_state;
                vector<int> v;
                //go EAST
                if(j != W) {
                    next_state = ((k & 1 << SOUTH) ? (1 << SOUTHWEST) : 0);
                    next_cost = (k & 1 << EAST) ? min_cost[i][j][k] : min_cost[i][j][k] + A[i][j + 1];
                    if(A[i - 1][j + 1] != 0 && !(k & 1 << NORTHEAST)) {
                        next_cost += A[i - 1][j + 1];
                        v.push_back(NORTH);
                    }
                    if(A[i][j + 2] != 0) {
                        next_cost += A[i][j + 2];
                        v.push_back(EAST);
                        next_state += (1 << EAST);
                    }
                    if(A[i + 1][j + 1] != 0) {
                        next_cost += A[i + 1][j + 1];
                        v.push_back(SOUTH);
                        next_state += (1 << SOUTH);
                    }
                    if(v.size() > 1) {
                        for(auto l: v) {
                            if(l == NORTH) min_cost[i][j + 1][next_state] = min(min_cost[i][j + 1][next_state], next_cost - A[i - 1][j + 1]);
                            else if(l == EAST) min_cost[i][j + 1][next_state - (1 << EAST)] = min(min_cost[i][j + 1][next_state - (1 << EAST)], next_cost - A[i][j + 2]);
                            else min_cost[i][j + 1][next_state - (1 << SOUTH)] = min(min_cost[i][j + 1][next_state - (1 << SOUTH)], next_cost - A[i + 1][j + 1]);
                        }
                    } else {
                        min_cost[i][j + 1][((k & 1 << SOUTH) ? (1 << SOUTHWEST) : 0)] =
                                min(min_cost[i][j + 1][((k & 1 << SOUTH) ? (1 << SOUTHWEST) : 0)],
                                    ((k & 1 << EAST) ? min_cost[i][j][k] : min_cost[i][j][k] + A[i][j + 1]));
                    }
                }
                //go SOUTH
                if(i != H) {
                    v.clear();
                    next_state = ((k & 1 << EAST) ? (1 << NORTHEAST) : 0);
                    next_cost = (k & 1 << SOUTH) ? min_cost[i][j][k] : min_cost[i][j][k] + A[i + 1][j];
                    if(A[i + 1][j - 1] != 0 && !(k & 1 << SOUTHWEST)) {
                        next_cost += A[i + 1][j - 1];
                        v.push_back(WEST);
                    }
                    if(A[i + 2][j] != 0) {
                        next_cost += A[i + 2][j];
                        v.push_back(SOUTH);
                        next_state += (1 << SOUTH);
                    }
                    if(A[i + 1][j + 1] != 0) {
                        next_cost += A[i + 1][j + 1];
                        v.push_back(EAST);
                        next_state += (1 << EAST);
                    }
                    if(v.size() > 1) {
                        for(auto l: v) {
                            if(l == WEST) min_cost[i + 1][j][next_state] = min(min_cost[i + 1][j][next_state], next_cost - A[i + 1][j - 1]);
                            else if(l == SOUTH) min_cost[i + 1][j][next_state - (1 << SOUTH)] = min(min_cost[i + 1][j][next_state - (1 << SOUTH)], next_cost - A[i + 2][j]);
                            else min_cost[i + 1][j][next_state - (1 << EAST)] = min(min_cost[i + 1][j][next_state - (1 << EAST)], next_cost - A[i + 1][j + 1]);
                        }
                    } else {
                        min_cost[i + 1][j][((k & 1 << EAST) ? (1 << NORTHEAST) : 0)] =
                                min(min_cost[i + 1][j][((k & 1 << EAST) ? (1 << NORTHEAST) : 0)],
                                    (k & 1 << SOUTH) ? min_cost[i][j][k] : min_cost[i][j][k] + A[i + 1][j]);
                    }
                }
            }
        }
    }

    cout << min_cost[H][W][0] << endl;

    return 0;
}