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 <iostream>
#include <fstream>
#include <sstream>
#include <iomanip> 
#include <complex> 
#include <string>
#include <vector> 
#include <list>
#include <deque> 
#include <stack> 
#include <queue> 
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm> 
#include <numeric> 
#include <typeinfo> 
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <cmath>
#include <climits> 
#include <ctime>
using namespace std;

int h,w,K;
string str;
int fie[100][100];
short dp[51][51][4][1<<12+1];
int res=0;

short dfs(int x,int y,int k,int bit,int pc){
	int nx=x,ny=y;
	if(dp[x][y][k][bit]!=-1)return dp[x][y][k][bit];
	
	//11nx++,10nx--,01ny++,00ny--;
	int nbit=bit;
	if(nbit >>11 & 1)nbit-=1<<11;
	if(nbit >>10 & 1)nbit-=1<<10;
	nbit<<=2;
	short r=-20000;
	if(k>=1){
		nbit+=2;
		x--;
		if(x>=0 && y>=0 && fie[x][y]!=-1){
			r=max(r,dfs(x,y,k-1,nbit,pc+1));
		}
		x++;
		y--;
		nbit-=2;
		if(x>=0 && y>=0 && fie[x][y]!=-1){
			r=max(r,dfs(x,y,k-1,nbit,pc+1));
		}
		y++;
	}
	nbit+=3;
	x++;
	if(x>=0 && y>=0 && fie[x][y]!=-1){
		r=max(r,dfs(x,y,k,nbit,pc+1));
	}
	x--;
	nbit-=2;
	y++;
	if(x>=0 && y>=0 && fie[x][y]!=-1){
		r=max(r,dfs(x,y,k,nbit,pc+1));
	}
	y--;
	
	int sum=0;
	bool f=false;
	for(int i=0;i<min(pc,6);i++){
		if(bit >>i*2 & 1){
			if(bit >>(i*2+1) & 1)nx--;
			else ny--;
		}else{
			if(bit >>(i*2+1) & 1)nx++;
			else ny++;
		}
		if(nx==x && ny==y){
			f=true;
			break;
		}
	}
	if(!f)sum+=fie[x][y];
	
	if(r<0 && x==w-1 && y==h-1)r=0;
	return (dp[x][y][k][bit]=sum+r);
}

int main(void){
	scanf("%d%d%d",&h,&w,&K);
	memset(fie,-1,sizeof(fie));
	memset(dp,-1,sizeof(dp));
	for(int i=0;i<h;i++){
		cin >> str;
		for(int j=0;j<w;j++){
			if(str[j]=='.')fie[j][i]=0;
			else if(str[j]=='#')fie[j][i]=-1;
			else fie[j][i]=str[j]-'0';
		}
	}
	printf("%d\n",dfs(0,0,K,0,0));
	return 0;
}