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;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=1;i<=n;i++)
#define drep(i,n) for(int i=n;i>=0;i--)
#define MAX 1004
#define B 100
#define dpb push_back
#define fi first
#define se second
typedef pair<int, int> P;
typedef long long ll;
const int inf = 1e9;
const ll INF = 1e18;
//__gcd(a,b), __builtin_popcount(a);

char str[MAX][MAX];
int a[MAX][MAX], dp[MAX][MAX][16];
int main(){
	int h, w;
	fill((int*)dp, (int*)(dp+MAX), inf);
	scanf("%d%d", &h, &w);
	rep(i,h)scanf("%s", str[i]);
	rrep(i,h)rrep(j,w){
		if(str[i-1][j-1] == '.')a[i][j] = 0;
		else a[i][j] = str[i-1][j-1]-'0';
	}
	
	dp[1][1][0] = 0;
	int b, c, t[4], cost, ans = inf;
	for(int y = 1;y <= h;y++){
		for(int x = 1;x <= w;x++){
			for(int bit = 0;bit < (1<<4);bit++){
				cost = dp[y][x][bit];
				if(cost == inf)continue;
				rep(i,4)t[i] = bit>>i&1;
				
				b = 6+8*t[2];
				c = a[y][x+1]*(!t[1])+a[y+1][x+1]+a[y][x+2];
				dp[y][x+1][b] = min(dp[y][x+1][b], cost+c);
				
				b = 4+8*t[2];
				c = a[y][x+1]*(!t[1])+a[y-1][x+1]*(!t[0])+a[y+1][x+1];
				dp[y][x+1][b] = min(dp[y][x+1][b], cost+c);
				
				b = 2+8*t[2];
				c = a[y][x+1]*(!t[1])+a[y-1][x+1]*(!t[0])+a[y][x+2];
				dp[y][x+1][b] = min(dp[y][x+1][b], cost+c);
				
				b = 6+t[1];
				c = a[y+1][x]*(!t[2])+a[y+1][x+1]+a[y+2][x];
				dp[y+1][x][b] = min(dp[y+1][x][b], cost+c);
				
				b = 2+t[1];
				c = a[y+1][x]*(!t[2])+a[y+1][x-1]*(!t[3])+a[y+1][x+1];
				dp[y+1][x][b] = min(dp[y+1][x][b], cost+c);
				
				b = 4+t[1];
				c = a[y+1][x]*(!t[2])+a[y+1][x-1]*(!t[3])+a[y+2][x];
				dp[y+1][x][b] = min(dp[y+1][x][b], cost+c);
				
				if(y == h && w == x)ans = min(ans, cost);
			}
		}
	}
	
	printf("%d\n", ans);
	return 0;
}