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 <bits/stdc++.h>
using namespace std;

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define mp(a,b) make_pair((a),(b))
#define debug(x) #x << "=" << (x)
 
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int INF=1<<30;
const long long int INF_=1LL<<58;
const double EPS=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; //00右,01下,10左,11上

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

int h,w,k,mask,memo[50][50][4][1<<12];
char fld[50][50];

bool Check(int y,int x){
	return 0<=y&&y<h&&0<=x&&x<w&&fld[y][x]!='#';
}

int Max(int y,int x,int c,int bit){
	int &res=memo[y][x][c][bit];
	if(res!=-1) return res;
	if(y==h-1&&x==w-1&&c==0) return res=0;

	bool done=false;
	int py=y,px=x;
	rep(i,0,2*k){
		py-=dy[(bit>>(i*2))&3];
		px-=dx[(bit>>(i*2))&3];
		if(y==py&&x==px){
			done=true;
			break;
		}
	}
	int gift=0;
	if(!done&&fld[y][x]!='.') gift=fld[y][x]-'0';

	res=-INF;
	rep(i,0,4){
		int ny=y+dy[i],nx=x+dx[i];
		if(!Check(ny,nx)) continue;
		if(i<2){
			res=max(res,Max(ny,nx,c,((bit<<2)|i)&mask)+gift);
		}else if(c>=1){
			res=max(res,Max(ny,nx,c-1,((bit<<2)|i)&mask)+gift);
		}
	}

	return res;
}

void Solve(){
	fill_n((int*)memo,50*50*4*(1<<12),-1);
	cin >> h >> w >> k;
	mask=(1<<(4*k))-1;
	rep(i,0,h) rep(j,0,w) cin >> fld[i][j];

	cout << Max(0,0,k,0) << endl;
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	Solve();
	return 0;
}