Problem: problem

JOI, who came to Australia for a trip, enjoyed sightseeing in various places and finally returned to Japan. Now, JOI is in the town with the international airport where the return flight departs. The town is divided into north, south, east and west, and each section has roads, souvenir shops, houses, and an international airport. JOI starts from the northwestern section and heads for the international airport in the southeastern section.

JOI can move from the current parcel to an adjacent parcel, but cannot enter a parcel with a house. Also, move only to the east or south section of the current section in order to make it in time for the flight. However, there is some time to spare, so you can move to the north or west section of the section you are up to K times.

When JOI enters the area where the souvenir shop is located, he buys souvenirs for his Japanese friends. JOI did a lot of research on the souvenir shops, so he knows which souvenir shop to buy and how many souvenirs to buy. Create a program to find the maximum number of souvenirs you can buy.

However, the time to buy souvenirs can be ignored, and if you visit the same souvenir shop more than once, you will only buy souvenirs the first time you visit.





Example

Input

5 4 2
...#
.#.#
.#73
8##.
....


Output

11

Solution (in CPP):

Solution:
#include <cstdio>
#include <cstring>
#include <algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int INF=1<<29;
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int h,w;
char B[50][51];

#define inv(d) (((d)+2)&3) // opposite direction

int dp[50][50][4][4][4][4][4][4][4];
int dfs(int i,int j,int k,int d0,int d1,int d2,int d3,int d4,int d5){
	int &res=dp[i][j][k][d0][d1][d2][d3][d4][d5];
	if(~res) return res;

	if(i==h-1 && j==w-1) res=0; // goal

	int y[6],x[6];
	y[0]=  i +dy[inv(d0)]; x[0]=  j +dx[inv(d0)];
	y[1]=y[0]+dy[inv(d1)]; x[1]=x[0]+dx[inv(d1)];
	y[2]=y[1]+dy[inv(d2)]; x[2]=x[1]+dx[inv(d2)];
	y[3]=y[2]+dy[inv(d3)]; x[3]=x[2]+dx[inv(d3)];
	y[4]=y[3]+dy[inv(d4)]; x[4]=x[3]+dx[inv(d4)];
	y[5]=y[4]+dy[inv(d5)]; x[5]=x[4]+dx[inv(d5)];

	rep(d,4){
		int ii=i+dy[d],jj=j+dx[d],kk=k;
		if(d==1 || d==2) kk--;
		if(kk<0) continue;
		if(0<=ii && ii<h && 0<=jj && jj<w && B[ii][jj]!='#'){
			bool vis=false;
			rep(l,6) if(ii==y[l] && jj==x[l]) vis=true;
			res=max(res,dfs(ii,jj,kk,d,d0,d1,d2,d3,d4)+(vis||B[ii][jj]=='.'?0:B[ii][jj]-'0'));
		}
	}

	if(res==-1) res=-INF;

	return res;
}

int main(){
	int k; scanf("%d%d%d",&h,&w,&k);
	rep(i,h) scanf("%s",B[i]);

	memset(dp,-1,sizeof dp);
	printf("%d\n",dfs(0,0,k,3,3,3,3,3,3));

	return 0;
}