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(i, 0, n)
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define LAR(a, b) ((a)=max((a), (b)))
#define SML(a, b) ((a)=min((a), (b)))

#ifdef LOCAL
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define E(a, b) (s[(a)][(b)]!='.')
#define N 1005
inline int B(int a){
	int ret=0;
	REP(i, 8){
		ret*=10;
		if(a&1) ret+=1;
		a>>=1;
	}
	return ret;
}
int h, w;
char s[N][N], m[N][N];
int dx[8]={-1,-1,-1, 0, 1, 1, 1, 0,};
int dy[8]={ 1, 0,-1,-1,-1, 0, 1, 1,};
int memo[N][N][32];
int DP(int, int, int);
inline int dp(int p, int q, int r){
	// DEBUG(" (%d, %d, %02x)\n", p, q, r);
	r&=m[p][q];
	if(~memo[p][q][r]) return memo[p][q][r];
	else return memo[p][q][r]=DP(p, q, r|0xe0);
}
#define INF 1000000000
int DP(int p, int q, int r){
	int ret=0;
	if(p==h+1 && q==w){
		ret=0;
		ret=0;
	}else if(p>h || q>w){
		ret=INF;
	}else{
		ret=INT_MAX;
		int d, t;
		bool ok=false;
		for(int i=1; i<10; i+=2){
			if(i==9 && ok) break;
			d=0; t=r;
			if(i<8){
				if(!(r&(1<<i))) continue;
				if(!E(p+dy[i], q+dx[i])) continue;
				for(int j=1; j<8; j+=2){
					if(i==j) continue;
					if(!(r&(1<<j))) continue;
					int a=p+dy[j], b=q+dx[j];
					if(!E(a, b)) continue;
					t^=(1<<j);
					d+=s[a][b];
				}
			}
			int c;
			c=0x10; //2=false, 4=true
			if(t&(1<<7)) c|=1<<0;
			if(t&(1<<3)) c|=1<<2;
			if(t&(1<<4)) c|=1<<3;
			SML(ret, d+((t&(1<<5))&&E(p, q+1)?s[p][q+1]:0)+dp(p, q+1, c));
			DEBUG(" (%d, %d, %08d)-(%d, R):%d+%d+%d(%d, %d, %08d)\n", p, q, B(r), i,
					d, ((t&(1<<5))&&E(p, q+1)?s[p][q+1]:0), dp(p, q+1, c), p, q+1, B(c));
			c=0x01; //3=false, 0=true
			if(t&(1<<0)) c|=1<<1;
			if(t&(1<<1)) c|=1<<2;
			if(t&(1<<5)) c|=1<<4;
			SML(ret, d+((t&(1<<7))&&E(p+1, q)?s[p+1][q]:0)+dp(p+1, q, c));
			DEBUG(" (%d, %d, %08d)-(%d, U):%d+%d+%d(%d, %d, %08d)\n", p, q, B(r), i,
					d, ((t&(1<<7))&&E(p+1, q)?s[p+1][q]:0), dp(p+1, q, c), p+1, q, B(c));
			ok=true;
		}
	}
	DEBUG("DP(%d, %d, %08d)=%d\n", p, q, B(r), ret);
	return ret;
}

int main(){
	scanf("%d%d", &h, &w);
	REP(i, w+2) s[0][i]=s[h+1][i]='.';
	REP(i, h){
		scanf("%s", s[i+1]+1);
		REP(j, w){
			char &t=s[i+1][j+1];
			if(t!='.') t-='0';
		}
		s[i+1][0]=s[i+1][w+1]='.';
	}
	FOR(i, 1, h+1){
		FOR(j, 1, w+1){
			m[i][j]=0;
			REP(k, 5){
				if(E(i+dy[k], j+dx[k])) m[i][j]|=(1<<k);
			}
		}
	}
	REP(i, N) REP(j, N) REP(k, 32) memo[i][j][k]=-1;
	printf("%d\n", dp(1, 1, 0x1f));
}