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;
typedef pair<int,int> p;
typedef vector<p> vp;
typedef tuple<int,int,int,vp> state;
#define rep(i,n)	for(int i=0;i<n;i++)
#define mp	make_pair
#define mt	make_tuple

map<state,int> dp;
vector<string> s;
int h,w;
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};

void makeWall(){
	string a="";
	rep(i,s[0].size())	a+="#";
	s.insert(s.begin(),a);
	s.push_back(a);
	rep(i,s.size())	s[i]="#"+s[i]+"#";
}

int solve(int x,int y,int k,vp used){
	if(x==w&&y==h&&k==0)	return 0;
	state cur=mt(x,y,k,used);
	if(dp.find(cur)!=dp.end())	return dp[cur];
	int ret=INT_MIN;
	rep(d,4){
		int nx=x+dx[d];
		int ny=y+dy[d];
		int nk=k-(d>1);
		if(nk<0||s[ny][nx]=='#')	continue;
		vp nused;
		rep(i,used.size()){
			if((nx+ny)-(used[i].first+used[i].second)<=nk){
				nused.push_back(used[i]);
			}
		}
		int val=0;
		if(s[ny][nx]!='.'){
			auto it=lower_bound(nused.begin(),nused.end(),mp(nx,ny));
			if(it==nused.end()||*it!=mp(nx,ny)){
				val=s[ny][nx]-'0';
				nused.insert(it,mp(nx,ny));
			}
		}
		ret=max(ret,val+solve(nx,ny,nk,nused));
	}
	return dp[cur]=ret;
}

int main(){
	int k;	cin>>h>>w>>k;
	s.resize(h);
	rep(i,h)	cin>>s[i];
	makeWall();
	cout<<solve(1,1,k,vp())<<endl;
	return 0;
}