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 <cstdio>
#include <algorithm>
#include <string>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define INF (1<<29)

int dp[1000][1000][16];
int h, w;
string str[1000];
int cost[1000][1000];

void update(int &a, int x) {
	a = min(a, x);
}

bool check(int x, int y) {
	return x >= 0 && x < w && y >= 0 && y < h;
}

int main() {
	cin >> h >> w;
	REP(i, h) cin >> str[i];
	REP(i, h) REP(j, w) {
		if (str[i][j] == '.') cost[i][j] = 0;
		else cost[i][j] = str[i][j] - '0';
	}
	
	fill(dp[0][0], dp[h][0], INF);
	
	dp[0][0][0] = 0;
	REP(i, h) REP(j, w) REP(k, 1<<4) {
		if (dp[i][j][k] == INF) continue;
		
		int c[3];
		int nextc, nextk, buy;
		
		// move down
		if (i < h - 1) {
			c[0] = check(j + 1, i + 1) ? cost[i + 1][j + 1] : 0; // right
			c[1] = check(j, i + 2)     ? cost[i + 2][j]     : 0; // down
			c[2] = check(j - 1, i + 1) ? cost[i + 1][j - 1] : 0; // left

			buy = 7; // ????????????????????? right, down, left
			if (!c[0]) buy -= 1;
			if (!c[1]) buy -= 2;
			if (!c[2] || k & 8) buy -= 4;
			
			nextc = dp[i][j][k] + (!(k & 4) ? cost[i + 1][j] : 0);
			nextk = k & 2 ? 1 : 0;
			
			if (buy == 0 || buy == 1 || buy == 2 || buy == 4) {
				update(dp[i + 1][j][nextk], nextc);
			}
			else if (buy == 3 || buy == 6 || buy == 5) {
				int tmp[3] = {2,4,0};
				REP(l, 3) if (1 << l & buy)
					update(dp[i + 1][j][nextk | tmp[l]], nextc + c[l]);
			}
			else if (buy == 7) {
				// right + left
				update(dp[i + 1][j][nextk | 2], nextc + c[0] + c[2]);
				// right + down
				update(dp[i + 1][j][nextk | 6], nextc + c[0] + c[1]);
				// down + left
				update(dp[i + 1][j][nextk | 4], nextc + c[1] + c[2]);
			}
			
		}
		
		// move right
		if (j < w - 1) {
			c[0] = check(j + 2, i)     ? cost[i][j + 2]     : 0; // right
			c[1] = check(j + 1, i + 1) ? cost[i + 1][j + 1] : 0; // down
			c[2] = check(j + 1, i - 1) ? cost[i - 1][j + 1] : 0; // up
			
			buy = 7; // right, down, up
			if (!c[0]) buy -= 1;
			if (!c[1]) buy -= 2;
			if (!c[2] || k & 1) buy -= 4;
			
			nextc = dp[i][j][k] + (!(k & 2) ? cost[i][j + 1] : 0);
			nextk = k & 4 ? 8 : 0;
			
			if (buy == 0 || buy == 1 || buy == 2 || buy == 4) {
				update(dp[i][j + 1][nextk], nextc);
			}
			else if (buy == 3 || buy == 6 || buy == 5) {
				int tmp[3] = {2,4,0};
				REP(l, 3) if (1 << l & buy)
					update(dp[i][j + 1][nextk | tmp[l]], nextc + c[l]);
			}
			else if (buy == 7) {
				// right + up
				update(dp[i][j + 1][nextk | 2], nextc + c[0] + c[2]);
				// right + down
				update(dp[i][j + 1][nextk | 6], nextc + c[0] + c[1]);
				// down + up
				update(dp[i][j + 1][nextk | 4], nextc + c[1] + c[2]); 
			}
		}
	}
	
	cout << dp[h - 1][w - 1][0] << endl;
	return 0;
}