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 "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "functional"

using namespace std;

constexpr long long int MOD = 1000000007;
//const int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;

//const double EPS = 1e-8;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> H >> W;
	vector<string>s(H);
	for (auto &i : s)cin >> i;
	vector<vector<int>>v(H + 2, vector<int>(W + 2));
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			if (s[i - 1][j - 1] == '.')continue;
			v[i][j] = s[i - 1][j - 1] - '0';
		}
	}
	int dy[] = { -1,0,1,1 };
	int dx[] = { 1,1,0,-1 };
	s[0] = s.back() = string(W + 2, '.');
	vector<vector<vector<int>>>dp(H + 2, vector<vector<int>>(W + 2, vector<int>(16, MOD)));
	dp[1][1][2] = v[1][1] + v[2][1];
	dp[1][1][4] = v[1][1] + v[1][2];
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			for (int k = 0; k < 16; k++) {
				if (j < W) {
					int add = 0;
					if ((k >> 1) & 1)add = v[i][j + 1];
					if ((k >> 0) & 1) {
						int nx = 1;
						if ((k >> 2) & 1)nx |= 1 << 3;
						dp[i][j + 1][nx] = min(dp[i][j + 1][nx], dp[i][j][k] + add + v[i][j + 2] + v[i + 1][j + 1]);
						add += v[i - 1][j + 1];
					}
					{
						int nx = 1 << 1;
						nx |= 1 << 0;
						if ((k >> 2) & 1)nx |= 1 << 3;
						dp[i][j + 1][nx] = min(dp[i][j + 1][nx], dp[i][j][k] + add + v[i + 1][j + 1]);
					}
					{
						int nx = 1 << 2;
						nx |= 1 << 0;
						if ((k >> 2) & 1)nx |= 1 << 3;
						dp[i][j + 1][nx] = min(dp[i][j + 1][nx], dp[i][j][k] + add + v[i][j + 2]);
					}
				}
				if (i < H) {
					int add = 0;
					if ((k >> 2) & 1)add = v[i + 1][j];
					if ((k >> 3) & 1) {
						int nx = 1 << 3;
						if ((k >> 1) & 1)nx |= 1 << 0;
						dp[i + 1][j][nx] = min(dp[i + 1][j][nx], dp[i][j][k] + add + v[i + 2][j] + v[i + 1][j + 1]);
						add += v[i + 1][j - 1];
					}
					{
						int nx = 1 << 1;
						nx |= 1 << 3;
						if ((k >> 1) & 1)nx |= 1 << 0;
						dp[i + 1][j][nx] = min(dp[i + 1][j][nx], dp[i][j][k] + add + v[i + 2][j]);
					}
					{
						int nx = 1 << 2;
						nx |= 1 << 3;
						if ((k >> 1) & 1)nx |= 1 << 0;
						dp[i + 1][j][nx] = min(dp[i + 1][j][nx], dp[i][j][k] + add + v[i + 1][j + 1]);
					}
				}
			}
		}
	}
	cout << *min_element(dp[H][W].begin(), dp[H][W].end()) << endl;
}