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<stdio.h>
#include<algorithm>
using namespace std;
int dp[50][50][1<<12][4];
char str[50][51];
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int main(){
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	for(int i=0;i<a;i++)scanf("%s",str[i]);
	for(int i=0;i<a;i++)
		for(int j=0;j<b;j++)
			if(str[i][j]=='.')str[i][j]='0';
	for(int i=0;i<a;i++)
		for(int j=0;j<b;j++)
			for(int k=0;k<(1<<12);k++)
				for(int l=0;l<4;l++)
					dp[i][j][k][l]=-99999999;
	dp[0][0][0][c]=0;
	for(int i=c;i>=0;i--){
		for(int j=0;j<a;j++){
			for(int k=0;k<b;k++){
				for(int l=0;l<(1<<12);l++){
					if(dp[j][k][l][i]<0)continue;
					int R;
					int W;
					bool ok;
					if(j<a-1&&str[j+1][k]!='#'){
						R=0;W=0;ok=true;
						for(int m=0;m<6;m++){
							R-=dx[(l>>(m*2))&3];
							W-=dy[(l>>(m*2))&3];
							if(R==1&&W==0)ok=false;
						}
						if(ok){
							dp[j+1][k][(l<<2)&((1<<12)-1)][i]=max(dp[j][k][l][i]+str[j+1][k]-'0',dp[j+1][k][(l<<2)&((1<<12)-1)][i]);
						}else{
							dp[j+1][k][(l<<2)&((1<<12)-1)][i]=max(dp[j][k][l][i],dp[j+1][k][(l<<2)&((1<<12)-1)][i]);
						}
					}
					if(k<b-1&&str[j][k+1]!='#'){
						R=0;W=0;ok=true;
						for(int m=0;m<6;m++){
							R-=dx[(l>>(m*2))&3];
							W-=dy[(l>>(m*2))&3];
							if(R==0&&W==1)ok=false;
						}
						if(ok){
							dp[j][k+1][((l<<2)+1)&((1<<12)-1)][i]=max(dp[j][k][l][i]+str[j][k+1]-'0',dp[j][k+1][((l<<2)+1)&((1<<12)-1)][i]);
						}else{
							dp[j][k+1][((l<<2)+1)&((1<<12)-1)][i]=max(dp[j][k][l][i],dp[j][k+1][((l<<2)+1)&((1<<12)-1)][i]);
						}
					}
					if(j&&str[j-1][k]!='#'&&i){
						R=0;W=0;ok=true;
						for(int m=0;m<6;m++){
							R-=dx[(l>>(m*2))&3];
							W-=dy[(l>>(m*2))&3];
							if(R==-1&&W==0)ok=false;
						}
						if(ok){
							dp[j-1][k][((l<<2)+2)&((1<<12)-1)][i-1]=max(dp[j][k][l][i]+str[j-1][k]-'0',dp[j-1][k][((l<<2)+2)&((1<<12)-1)][i-1]);
						}else{
							dp[j-1][k][((l<<2)+2)&((1<<12)-1)][i-1]=max(dp[j][k][l][i],dp[j-1][k][((l<<2)+2)&((1<<12)-1)][i-1]);
						}
					}
					if(k&&str[j][k-1]!='#'&&i){
						R=0;W=0;ok=true;
						for(int m=0;m<6;m++){
							R-=dx[(l>>(m*2))&3];
							W-=dy[(l>>(m*2))&3];
							if(R==0&&W==-1)ok=false;
						}
						if(ok){
							dp[j][k-1][((l<<2)+3)&((1<<12)-1)][i-1]=max(dp[j][k][l][i]+str[j][k-1]-'0',dp[j][k-1][((l<<2)+3)&((1<<12)-1)][i-1]);
						}else{
							dp[j][k-1][((l<<2)+3)&((1<<12)-1)][i-1]=max(dp[j][k][l][i],dp[j][k-1][((l<<2)+3)&((1<<12)-1)][i-1]);
						}
					}
				}
			}
		}
	}
	int ret=0;
	for(int i=0;i<=c;i++)
		for(int j=0;j<(1<<12);j++)ret=max(ret,dp[a-1][b-1][j][i]);
	printf("%d\n",ret);
}