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
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 1012345678;
int A[1006][1006], dp[1006][1006][9];
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int H, W;
	cin >> H >> W;
	for (int i = 0; i < H; ++i) {
		string S;
		cin >> S;
		for (int j = 0; j < W; ++j) {
			if (S[j] != '.') {
				A[i + 1][j + 1] = S[j] - '0';
			}
		}
	}
	for (int i = 1; i <= H; ++i) {
		for (int j = 1; j <= W; ++j) {
			for (int k = 0; k < 9; ++k) {
				dp[i][j][k] = inf;
			}
		}
	}
	dp[1][1][0] = A[0][0] + A[1][0];
	dp[1][1][1] = A[0][0] + A[0][1];
	for (int i = 1; i <= H; ++i) {
		for (int j = 1; j <= W; ++j) {
			if (j < W) {
				dp[i][j + 1][0] = min(dp[i][j + 1][0], A[i + 1][j + 1] + min({ dp[i][j][1] + A[i - 1][j + 1], dp[i][j][4] + A[i - 1][j + 1], dp[i][j][7] }));
				dp[i][j + 1][1] = min(dp[i][j + 1][1], A[i][j + 2] + min({ dp[i][j][1] + A[i - 1][j + 1], dp[i][j][4] + A[i - 1][j + 1] , dp[i][j][7] }));
				dp[i][j + 1][2] = min(dp[i][j + 1][2], A[i][j + 2] + A[i + 1][j + 1] + min({ dp[i][j][1], dp[i][j][4], dp[i][j][7] }));
				dp[i][j + 1][3] = min(dp[i][j + 1][3], A[i + 1][j + 1] + min({ dp[i][j][0] + A[i][j + 1] + A[i - 1][j + 1], dp[i][j][2] + A[i - 1][j + 1], dp[i][j][3] + A[i][j + 1] + A[i - 1][j + 1], dp[i][j][5] + A[i - 1][j + 1], dp[i][j][6] + A[i][j + 1], dp[i][j][8] }));
				dp[i][j + 1][4] = min(dp[i][j + 1][4], A[i][j + 2] + min({ dp[i][j][0] + A[i][j + 1] + A[i - 1][j + 1], dp[i][j][2] + A[i - 1][j + 1], dp[i][j][3] + A[i][j + 1] + A[i - 1][j + 1], dp[i][j][5] + A[i - 1][j + 1], dp[i][j][6] + A[i][j + 1], dp[i][j][8] }));
				dp[i][j + 1][5] = min(dp[i][j + 1][5], A[i][j + 2] + A[i + 1][j + 1] + min({ dp[i][j][0] + A[i][j + 1] , dp[i][j][2], dp[i][j][3] + A[i][j + 1], dp[i][j][5], dp[i][j][6] + A[i][j + 1], dp[i][j][8] }));
			}
			if (i < H) {
				dp[i + 1][j][0] = min(dp[i + 1][j][0], A[i + 2][j] + min({ dp[i][j][0] + A[i + 1][j - 1], dp[i][j][3], dp[i][j][6] + A[i + 1][j - 1] }));
				dp[i + 1][j][1] = min(dp[i + 1][j][1], A[i + 1][j + 1] + min({ dp[i][j][0] + A[i + 1][j - 1], dp[i][j][3], dp[i][j][6] + A[i + 1][j - 1] }));
				dp[i + 1][j][2] = min(dp[i + 1][j][2], A[i + 2][j] + A[i + 1][j + 1] + min({ dp[i][j][0], dp[i][j][3], dp[i][j][6] }));
				dp[i + 1][j][6] = min(dp[i + 1][j][6], A[i + 2][j] + min({ dp[i][j][1] + A[i + 1][j] + A[i + 1][j - 1], dp[i][j][2] + A[i + 1][j - 1], dp[i][j][4] + A[i + 1][j], dp[i][j][5], dp[i][j][7] + A[i + 1][j] + A[i + 1][j - 1], dp[i][j][8] + A[i + 1][j - 1] }));
				dp[i + 1][j][7] = min(dp[i + 1][j][7], A[i + 1][j + 1] + min({ dp[i][j][1] + A[i + 1][j] + A[i + 1][j - 1], dp[i][j][2] + A[i + 1][j - 1], dp[i][j][4] + A[i + 1][j], dp[i][j][5], dp[i][j][7] + A[i + 1][j] + A[i + 1][j - 1], dp[i][j][8] + A[i + 1][j - 1] }));
				dp[i + 1][j][8] = min(dp[i + 1][j][8], A[i + 2][j] + A[i + 1][j + 1] + min({ dp[i][j][1] + A[i + 1][j], dp[i][j][2], dp[i][j][4] + A[i + 1][j], dp[i][j][5], dp[i][j][7] + A[i + 1][j], dp[i][j][8] }));
			}
		}
	}
	int ans = *min_element(dp[H][W], dp[H][W] + 9);
	cout << ans << endl;
	return 0;
}
