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 dp[1010][1010][16];

const int dx[] = { 1,0,-1,0 };
const int dy[] = { 0,1,0,-1 };

int main() {
	int H, W; cin >> H >> W;
	for (int i = 0; i < 1010; ++i)for (int j = 0; j < 1010; ++j)for (int k = 0; k < 16; ++k){
		dp[i][j][k] = 1e9;
	}
	dp[5][5][0] = 0;
	vector<vector<int>>field(H + 8, vector<int>(W + 8));
	for (int i = 0; i < H; ++i) {
		string st; cin >> st;
		for (int j = 0; j < W; ++j) {
			if (st[j] != '.') {
				field[i + 5][j + 5] = st[j] - '0';
			}
		}
	}
	for (int nowy = 5; nowy < H + 6; ++nowy) {
		for (int nowx = 5; nowx < W +6; ++nowx) {
			for (int n = 0; n < 16; ++n) {
				const int eat_ld = n & 1;
				const int eat_d = (n & 2) >> 1;
				const int eat_r = (n & 4) >> 2;
				const int eat_ru = (n & 8) >> 3;
				if (1e9 <= dp[nowy][nowx][n])continue;
				vector<vector<char>>baseeats(4, vector<char>(4));
				baseeats[2][0] = eat_ld;
				baseeats[2][1] = eat_d;
				baseeats[1][2] = eat_r;
				baseeats[0][2] = eat_ru;
				baseeats[1][1] = true;
				for (int way = 0; way < 2; ++way) {
					const int  nextx = nowx + dx[way];
					const int nexty = nowy + dy[way];
					const int ax = 1 + dx[way];
					const int ay = 1 + dy[way];
					for (int w = 0; w < 4; ++w) {
						int nexteat_ld = baseeats[2 + dy[way]][0 + dx[way]];
						int nexteat_d = baseeats[2 + dy[way]][1 + dx[way]];
						int nexteat_r = baseeats[1 + dy[way]][2 + dx[way]];
						int nexteat_ru = baseeats[0 + dy[way]][2 + dx[way]];
						int eat_sum = 0;
						if (!baseeats[ay][ax]) {
							if (ay == 2 + dy[way] && ax == 0 + dx[way])nexteat_ld = true;
							if (ay == 2 + dy[way] && ax == 1 + dx[way])nexteat_d = true;
							if (ay == 1 + dy[way] && ax == 2 + dx[way])nexteat_r = true;
							if (ay == 0 + dy[way] && ax == 2 + dx[way])nexteat_ru = true;
							eat_sum += field[nexty][nextx];
						}
						for (int v = 0; v < 4; ++v) {
							if (v == w)continue;
							else {
								if (!baseeats[ay + dy[v]][ax + dx[v]]) {
									if (ay + dy[v]== 2 + dy[way] && ax + dx[v] == 0 + dx[way])nexteat_ld = true;
									if (ay + dy[v]== 2 + dy[way] && ax + dx[v] == 1 + dx[way])nexteat_d = true;
									if (ay + dy[v]== 1 + dy[way] && ax + dx[v] == 2 + dx[way])nexteat_r = true;
									if (ay + dy[v]== 0 + dy[way] && ax + dx[v] == 2 + dx[way])nexteat_ru = true;
									eat_sum += field[nexty + dy[v]][nextx + dx[v]];
								}
							}
						}
						int n = dp[nowy][nowx][eat_ld+2*eat_d+4*eat_r+8*eat_ru] + eat_sum;
						dp[nexty][nextx][nexteat_ld+2*nexteat_d+4*nexteat_r+8*nexteat_ru] = min(dp[nexty][nextx][nexteat_ld + 2 * nexteat_d + 4 * nexteat_r + 8 * nexteat_ru], n);
					}
				}
			}
		}
	}
	int ans = 1e9;
	for (int l = 0; l < 16; ++l) {
		ans = min(ans, dp[H + 4][W + 4][l]);	
	}
	cout << ans << endl;

	return 0;
}