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<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<random>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
#include<stdlib.h>
#include<unordered_map>
#include<time.h>
#define int long long
#define rep(i,a,n) for (int (i)=(a);(i)<(n);(i)++)
using namespace std;
typedef long long ll;
typedef long double ld;
using namespace std;
int h, w, a[1000][1000];
int dp[1000][1000][32];
int solve(int x, int y, int s) {
	if (x == h - 1 && y == w - 1)
		return 0;
	if (dp[x][y][s] != -1)
		return dp[x][y][s];
	int sum = 0, ans = 1145141919810;
	if (s & 1) sum += a[x][y];
	if (s & 2) sum += a[x - 1][y];
	if (s & 4) sum += a[x][y - 1];
	if (x + 1 < h)sum += a[x + 1][y];
	if (y + 1 < w)sum += a[x][y + 1];
	if (y + 1 < w) {//????????????
		if (sum == 0 || (sum == a[x][y] && s & 1)) {
			ans = min(ans, (sum + solve(x, y + 1, (s & 8 ? 2 : 0) + (x > 0 && y + 2 < w&&a[x - 1][y + 2] > 0 ? 8 : 0))));
		}
		else {
			if (s & 2)//??????????????????
				ans = min(ans, (sum - a[x - 1][y] + solve(x, y + 1, (s & 8 ? 2 : 0) + (y + 2 < w&&a[x - 1][y + 2] > 0 ? 8 : 0))));
			if (s & 4)//??????????????????
				ans = min(ans, (sum - a[x][y - 1] + solve(x, y + 1, (s & 8 ? 2 : 0) + (x > 0 && y + 2 < w&&a[x - 1][y + 2] > 0 ? 8 : 0))));
			if (x + 1 < h&&a[x + 1][y] > 0)//??????????????????
				ans = min(ans, (sum - a[x + 1][y] + solve(x, y + 1, 16 + (s & 8 ? 2 : 0) + (x > 0 && y + 2 < w&&a[x - 1][y + 2] > 0 ? 8 : 0))));
			if (y + 1 < w&&a[x][y + 1] > 0)//??????????????????
				ans = min(ans, (sum - a[x][y + 1] + solve(x, y + 1, 1 + (s & 8 ? 2 : 0) + (x > 0 && y + 2 < w&&a[x - 1][y + 2] > 0 ? 8 : 0))));
		}
	}
	if (x + 1 < h) {//????????????
		if (sum == 0 || (sum == a[x][y] && s & 1)) {
			ans = min(ans, (sum + solve(x + 1, y, (s & 16 ? 4 : 0) + (x + 2 < h&&y > 0 && a[x + 2][y - 1] > 0 ? 16 : 0))));
		}
		else {
			if (s & 2)//??????????????????
				ans = min(ans, (sum - a[x - 1][y] + solve(x + 1, y, (s & 16 ? 4 : 0) + (x + 2 < h&&y > 0 && a[x + 2][y - 1] > 0 ? 16 : 0))));
			if (s & 4)//??????????????????
				ans = min(ans, (sum - a[x][y - 1] + solve(x + 1, y, (s & 16 ? 4 : 0) + (x + 2 < h&&y > 0 && a[x + 2][y - 1] > 0 ? 16 : 0))));
			if (x + 1 < h&&a[x + 1][y] > 0)//??????????????????
				ans = min(ans, (sum - a[x + 1][y] + solve(x + 1, y, 1 + (s & 16 ? 4 : 0) + (x + 2 < h&&y > 0 && a[x + 2][y - 1] > 0 ? 16 : 0))));
			if (y + 1 < w&&a[x][y + 1] > 0)//??????????????????
				ans = min(ans, (sum - a[x][y + 1] + solve(x + 1, y, 8 + (s & 16 ? 4 : 0) + (x + 2 < h&&y > 0 && a[x + 2][y - 1] > 0 ? 16 : 0))));
		}
	}
	return dp[x][y][s] = ans;
}
signed main() {
	cin >> h >> w;
	for (int i = 0; i < h; i++) {
		string s;
		cin >> s;
		for (int j = 0; j < w; j++) {
			if (s[j] == '.')
				a[i][j] = 0;
			else
				a[i][j] = (int)s[j] - '0';
			for (int z = 0; z < 32; z++)
				dp[i][j][z] = -1;
		}
	}
	cout << solve(0, 0, 0) << endl;
	getchar(); getchar();
}