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>
#define INF 1e+9
using namespace std;

int field[1005][1005],dp[1005][1005][16];

int get(int x,int k){
	return ((x >> k) & 1);
}

int num(int a,int b,int c,int d){
	return a + b * 2 + c * 4 + d * 8;
}

signed main(){
	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] != '.') field[i + 1][j + 1] = s[j] - '0';
		}
	}
	for(int i = 0;i <= h;i++){
		for(int j = 0;j <= w;j++){
			for(int k = 0;k < 16;k++) dp[i][j][k] = INF;
		}
	}
	dp[1][1][0] = 0;
	for(int i = 1;i <= h;i++){
		for(int j = 1;j <= w;j++){
			for(int k = 0;k < 16;k++){
				int add = field[i + 1][j + 1] + field[i + 2][j];
				if(!get(k,1)) add += field[i + 1][j];
				if(!get(k,0)) add += field[i + 1][j - 1];
				dp[i + 1][j][num(0,0,1,get(k,2))] = min(dp[i + 1][j][num(0,0,1,get(k,2))],dp[i][j][k] + add - field[i + 2][j]);
				dp[i + 1][j][num(0,1,0,get(k,2))] = min(dp[i + 1][j][num(0,1,0,get(k,2))],dp[i][j][k] + add - field[i + 1][j + 1]);
				if(!get(k,0)) dp[i + 1][j][num(0,1,1,get(k,2))] = min(dp[i + 1][j][num(0,1,1,get(k,2))],dp[i][j][k] + add - field[i + 1][j - 1]);
				add = field[i + 1][j + 1] + field[i][j + 2];
				if(!get(k,2)) add += field[i][j + 1];
				if(!get(k,3)) add += field[i - 1][j + 1];
				dp[i][j + 1][num(get(k,1),1,0,0)] = min(dp[i][j + 1][num(get(k,1),1,0,0)],dp[i][j][k] + add - field[i][j + 2]);
				dp[i][j + 1][num(get(k,1),0,1,0)] = min(dp[i][j + 1][num(get(k,1),0,1,0)],dp[i][j][k] + add - field[i + 1][j + 1]);
				if(!get(k,3)) dp[i][j + 1][num(get(k,1),1,1,0)] = min(dp[i][j + 1][num(get(k,1),1,1,0)],dp[i][j][k] + add - field[i - 1][j + 1]);
			}
		}
	}
	int mi = INF;
	for(int i = 0;i < 16;i++) mi = min(mi,dp[h][w][i]);
	cout << mi << endl;
	return 0;
}