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<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<random>
#include<functional>
#include<utility>

typedef long long ll;

#define rep(i,n) for(int i=0; i<n; i++)
#define pb push_back
#define P pair<int, int>
#define PLI pair<ll, int>
#define PLL pair<ll, ll>
#define VI vector<int>
#define VII vector<VI>

using namespace std;


const int INF = 1e9;
const int mru = 1, mu = 1 << 1, ml = 1 << 2, mld = 1 << 3;

int ToBit(int a, int b, int c, int d) {
	return a + (b << 1) + (c << 2) + (d << 3);
}

int H, W;
int stl[1005][1005], dp[1005][1005][1<<4];
int main() {
	cin >> H >> W;
	rep(i, H) rep(j, W) {
		char c;
		cin >> c;
		stl[i + 1][j + 1] = (c == '.') ? 0 : (c - '0');
	}

	rep(i, H) rep(j, W) rep(k, 1<<4) dp[i+1][j+1][k] = INF;
	dp[1][1][0] = 0;
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			for (int bit = 0; bit < (1 << 4); bit++) {
				if (dp[i][j][bit] == INF) continue;
				bool isru = bit & mru;
				bool isu = bit & mu;
				bool isl = bit & ml;
				bool isld = bit & mld;

				int vu = (isu ? 0 : stl[i - 1][j]);
				int vl = (isl ? 0 : stl[i][j-1]);
				int vd = stl[i+1][j];
				int vr = stl[i][j+1];

				int nbit, nco;
				if (i < H) {
					nco = vd + vr + vu;
					nbit = ToBit(1, 1, isld, 0);
					dp[i + 1][j][nbit] = min(dp[i + 1][j][nbit], dp[i][j][bit] + nco);

					nco = vd + vr + vl;
					nbit = ToBit(1, 1, isld, 0);
					dp[i + 1][j][nbit] = min(dp[i + 1][j][nbit], dp[i][j][bit] + nco);

					nco = vd + vu + vl;
					nbit = ToBit(0, 1, isld, 0);
					dp[i + 1][j][nbit] = min(dp[i + 1][j][nbit], dp[i][j][bit] + nco);
				}
				if (j < W) {
					nco = vr + vu + vl;
					nbit = ToBit(0, isru, 1, 0);
					dp[i][j+1][nbit] = min(dp[i][j+1][nbit], dp[i][j][bit] + nco);

					nco = vr + vu + vd;
					nbit = ToBit(0, isru, 1, 1);
					dp[i][j + 1][nbit] = min(dp[i][j + 1][nbit], dp[i][j][bit] + nco);

					nco = vr + vl + vd;
					nbit = ToBit(0, isru, 1, 1);
					dp[i][j + 1][nbit] = min(dp[i][j + 1][nbit], dp[i][j][bit] + nco);
				}
			}
		}
	}
	int ans = INF;
	rep(i, 1 << 4) ans = min(ans, dp[H][W][i]);
	cout << ans << endl;

	return 0;
}