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;

int a, b;
int c[1000][1000];
bool d[1000][1000];//1 ?????£?????????
int dp[1000][1000][16];//1 ?????£???
int dx[4] = {-1,0,1,1}, dy[4] = {1,1,0,-1};
int ex[4]={-1,0,0,1},ey[4]={0,-1,1,0};
void decide(int x, int y, int z) {//z ????????§????????????
	if (d[x][y])z += c[x][y];
	bool t = true;
	for (int i = 0; i < 4; i++) {
		if (x + ex[i] >= 0 && x + ex[i] < a&&y + ey[i] >= 0 && y + ey[i] < b&&d[x + ex[i]][y + ey[i]]) {
			t = false;
			int o = z, bit = 0;
			int n[4], m[4], sum = 0;
			for (int j = 0; j < 4; j++) {
				if (i == j)continue;
				int xx = x + ex[j], yy = y + ey[j];
				if (xx >= 0 && xx < a&&yy >= 0 && yy < b&&d[xx][yy]) {
					o += c[xx][yy];
					d[xx][yy] = false;
					n[sum] = xx; m[sum] = yy;
					sum++;
				}
			}
			for (int j = 0; j < 4; j++) {
				int xx = x + dx[j], yy = y + dy[j];
				if (xx >= 0 && xx < a&&yy >= 0 && yy < b) {
					if (!d[xx][yy] && c[xx][yy]!=0) {
						bit |= (1<<j);
					}
				}
			}
			for (int j = 0; j < sum; j++) {
				d[n[j]][m[j]] = true;
			}
			dp[x][y][bit] = min(dp[x][y][bit], o);
		}
	}
	if (t) {
		int bit = 0;
		for (int j = 0; j < 4; j++) {
			int xx = x + dx[j], yy = y + dy[j];
			if (xx >= 0 && xx < a&&yy >= 0 && yy < b) {
				if (!d[xx][yy] && c[xx][yy]) {
					bit |= (1 << j);
				}
			}
		}
		dp[x][y][bit] = min(dp[x][y][bit], z);
	}
}
void kousin(int x,int y,int z) {
	for (int i = 0; i < 4; i++) {
		if (z&(1 << i)) {
			d[x + dx[i]][y + dy[i]] = false;
		}
	}
	bool S = false;
	if (c[x][y]) {
		d[x][y] = false;
		S = true;
	}
	if(x+1<a)decide(x + 1, y, dp[x][y][z]);
	if(y+1<b)decide(x, y+1, dp[x][y][z]);
	if (S) {
		d[x][y] = true;
	}
	for (int i = 0; i < 4; i++) {
		if (z&(1 << i)) {
			d[x + dx[i]][y + dy[i]] = true;
		}
	}
}
int main() {
	memset(dp, 0x3f, sizeof(dp));
	cin >> a >> b;
	for (int e = 0; e < a; e++) {
		string f; cin >> f;
		for (int g = 0; g < b; g++) {
			if (f[g] != '.') {
				c[e][g] = f[g] - '0';
				d[e][g] = true;
			}
		}
	}
	dp[0][0][0] = 0;
	for (int i = 0; i < a + b - 2; i++) {
		for (int x = 0; x < a&&x<=i; x++) {
			int y = i - x;
			if (y < 0 || y >= b)continue;
			for (int z = 0; z < 16; z++) {
				if (dp[x][y][z] != 1061109567) {
					kousin(x, y, z);
				}
			}
		}
	}
	int MIN = 1<<29;
	for (int i = 0; i < 16; i++) {
		MIN = min(MIN, dp[a - 1][b - 1][i]);
	}
	cout << MIN << endl;
}