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 <cstdio>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
int dir[] = { 0, 1, 0, -1 };
short H, W, a[1009][1009], dp[1009][1009][259]; bool used[1009][1009];
short solve(int x, int y) {
	if (x == H && y == W) return 0;
	int bit = used[x][y] + 2 * used[x - 1][y] + 4 * used[x][y - 1] + 8 * used[x - 1][y + 1] + 16 * used[x + 1][y - 1] + 32 * used[x - 1][y - 1] + 64 * used[x][y + 1] + 128 * used[x + 1][y];
	if (dp[x][y][bit] != 32767) return dp[x][y][bit];
	short ret = 32767;
	for (int i = 0; i < 2; i++) {
		int tx = x + dir[i], ty = y + dir[i ^ 1];
		if (tx <= H && ty <= W) {
			short val = 0, rbit = 0;
			if (!used[tx][ty]) val = a[tx][ty], used[tx][ty] = true, rbit = 16;
			for (int j = 0; j < 4; j++) {
				int ux = tx + dir[j], uy = ty + dir[j ^ 1];
				if (!used[ux][uy]) {
					used[ux][uy] = true;
					val += a[ux][uy];
					rbit |= 1 << j;
				}
			}
			for (int j = 0; j < 4; j++) {
				int ux = tx + dir[j], uy = ty + dir[j ^ 1];
				if (rbit & (1 << j)) val -= a[ux][uy], used[ux][uy] = false;
				ret = min(ret, (short)(val + solve(tx, ty)));
				if (rbit & (1 << j)) val += a[ux][uy], used[ux][uy] = true;
			}
			for (int j = 0; j < 4; j++) {
				if (rbit & (1 << j)) used[tx + dir[j]][ty + dir[j ^ 1]] = false;
			}
			if (rbit & 16) used[tx][ty] = false;
		}
	}
	return dp[x][y][bit] = ret;
}
char buf[1009];
int main() {
	scanf("%hd%hd", &H, &W);
	for (int i = 0; i < H; i++) {
		scanf("%s", buf);
		for (int j = 0; j < W; j++) {
			if (buf[j] != '.') a[i + 1][j + 1] = buf[j] - 48;
			fill(dp[i + 1][j + 1], dp[i + 1][j + 1] + 256, 32767);
		}
	}
	int ret = solve(1, 1);
	printf("%hd\n", ret);
	return 0;
}