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>
using namespace std;
class nfloor {
public:
	int sell;
	int buy[5];
	int dp[1<<5];
};

int inf = 200000;
int H, W;
int movex[]{ 1,0,-1,0,0 }, movey[]{ 0,1,0,-1,0 };
nfloor **map;

int search(int, int);

int main() {
	cin >> H >> W;
	map = new nfloor*[W + 2];
	for (int i = 0; i < W + 2; ++i) {
		map[i] = new nfloor[H + 2];
		for (int j = 0; j < H + 2; ++j) {
			map[i][j].sell = 0;
			for (int k = 0; k < 5; ++k) {
				map[i][j].buy[k] = 0;
			}
			for (int k = 0; k < 1<<5; ++k) {
				map[i][j].dp[k] = inf;
			}
		}
	}
	char dummy;
	for (int y = 1; y <= H; ++y) {
		for (int x = 1; x <= W; ++x) {
			cin >> dummy;
			map[x][y].sell = dummy == '.' ? 0 : dummy - '0';
		}
	}

	cout << search(1, 1) << endl;

	return 0;
}

int search(int x, int y) {
	if (x > W || y > H)return inf;
	else if (x == W&&y == H)return 0;
	int slide = 0;
	if (map[x][y - 1].sell == 0)slide += 1 << 2;
	if (map[x - 1][y].sell == 0)slide += 1 << 1;
	if (map[x - 1][y + 1].sell == 0)slide += 1 << 3;
	if (map[x + 1][y - 1].sell == 0)slide += 1 << 4;
	if (map[x][y].sell == 0)slide += 1;
	if (map[x][y].dp[slide] != inf)return map[x][y].dp[slide];
	int stock = 0;
	for (int i = 0; i < 5; ++i) {
		stock += map[x + movex[i]][y + movey[i]].sell;
		swap(map[x][y].buy[i], map[x + movex[i]][y + movey[i]].sell);
	}
	for (int i = 0; i < 4; ++i) {
		stock -= map[x][y].buy[i];
		swap(map[x][y].buy[i], map[x + movex[i]][y + movey[i]].sell);
		for (int j = 0; j < 2; ++j) {
			map[x][y].dp[slide] = min(map[x][y].dp[slide], search(x + movex[j], y + movey[j]) + stock);
		}
		swap(map[x][y].buy[i], map[x + movex[i]][y + movey[i]].sell);
		stock += map[x][y].buy[i];
	}
	for (int i = 0; i < 5; ++i) {
		swap(map[x][y].buy[i], map[x + movex[i]][y + movey[i]].sell);
	}
	return map[x][y].dp[slide];
}