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>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef pair<int, int>P;

char s[1000][1001];
int h, w, dp[1000][1000][1 << 5], dx[]{ -1,0,1,0 }, dy[]{ 0,-1,0,1 };
int a[]{ 0,2,4,3 }, b[]{ 0,2,3,1 };

int s1(int t) {
	int u = 0;
	if (t >> 3 & 1)u |= 1;
	if (t >> 4 & 1)u |= 1 << 2;
	return u;
}
int s2(int t) {
	int u = 0;
	if (t >> 1 & 1)u |= 1;
	if (t >> 3 & 1)u |= 1 << 2;
	return u;
}
int rec(int x, int y, int t) {
	int T = t;
	if (dp[x][y][t] != -1)return dp[x][y][t];
	int res = INT_MAX, p = 0;
	if ((!(t >> 3 & 1)) && '1' <= s[x][y] && s[x][y] <= '9') {
		t |= 1 << 3; p = s[x][y] - '0';
	}
	if (x == h - 1 && y == w - 1)return dp[x][y][t] = p;
	vector<P>v;
	rep(i, 4) {
		int nx = x + dx[i], ny = y + dy[i];
		if (nx < 0 || ny < 0 || nx >= h || ny >= w || s[nx][ny] < '1' || '9' < s[nx][ny])continue;
		if (i < 2 && (t >> a[i] & 1))continue;
		v.push_back(P(s[nx][ny] - '0', i));
	}
	if (v.empty()) {
		if (x + 1 < h)res = min(res, rec(x + 1, y, s1(t)) + p);
		if (y + 1 < w)res = min(res, rec(x, y + 1, s2(t)) + p);
	}
	rep(i, v.size()) {
		if (x + 1 < h) {
			int sum = p, r = t, u = 0;
			rep(j, v.size()) {
				if (i == j)continue;
				sum += v[j].first;
				if (v[j].second >= 2)u |= 1 << b[v[j].second];
				else r |= 1 << b[v[j].second];
			}
			res = min(res, rec(x + 1, y, s1(r) | u) + sum);
		}
		if (y + 1 < w) {
			int sum = p, r = t, u = 0;
			rep(j, v.size()) {
				if (i == j)continue;
				sum += v[j].first;
				if (v[j].second >= 2)u |= 1 << a[v[j].second];
				else r |= 1 << a[v[j].second];
			}
			res = min(res, rec(x, y + 1, s2(r) | u) + sum);
		}
	}
	return dp[x][y][T] = res;
}
int main() {
	scanf("%d%d", &h, &w);
	rep(i, h)scanf("%s", s[i]);
	memset(dp, -1, sizeof(dp));
	printf("%d\n", rec(0, 0, 0));
}