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 <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#ifdef __GNUC__
#define foreach(it,X) for(__typeof((X).begin()) it=(X).begin();it!=(X).end();it++)
#endif

#ifdef MSC_VER
#pragma comment(linker, "/STACK:400000000")
#define foreach(it,X) for(it=X.begin();it!=X.end();it++)
#endif

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define ite iterator
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define sec second
#define sz(x) ((int)x.size())
#define vc vector

typedef istringstream iss;
typedef long long ll;
typedef pair<int,int> pi;
typedef stringstream sst;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;

int H,W,K;
char c[55][55];
int dp[55][55][4][1<<12];
int dy[]={0,1,0,-1};
int dx[]={1,0,-1,0};

int f(int y,int x,int k,int mask){
	int& res=dp[y][x][k][mask];
	if(res!=-1)return res;
	if(y==H && x==W && k==0)return res=0;
	res=0;
	if(isdigit(c[y][x])){
		int Y=y,X=x,d;
		rep(i,2*K){
			d=mask>>(2*i)&3;
			Y-=dy[d],X-=dx[d];
			if(Y==y && X==x)goto fail;
		}
		res=c[y][x]-'0';
		fail:;
	}
	
	int res2=-INF,ny,nx;
	rep(d,2){
		ny=y+dy[d],nx=x+dx[d];
		if(c[ny][nx]!='#'){
			res2=max(res2,f(ny,nx,k,(mask<<2 | d)  & ((1<<4*K)-1)));
		}
	}
	if(k)rep2(d,2,4){
		ny=y+dy[d],nx=x+dx[d];
		if(c[ny][nx]!='#'){
			res2=max(res2,f(ny,nx,k-1,(mask<<2 | d)  & ((1<<4*K)-1)));
		}
	}
	return res+=res2;
}

int main(){
	ios_base::sync_with_stdio(0);
	
	cin>>H>>W>>K;
	rep(i,H+2)rep(j,W+2)c[i][j]='#';
	rep2(i,1,H+1)rep2(j,1,W+1)cin>>c[i][j];
	memset(dp,-1,sizeof(dp));
	cout<<f(1,1,K,0)<<endl;
}