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 <cstdio>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int dx[] = {-1,0,1,0};
int dy[] = {0,-1,0,1};

int H,W;
char c[50][50];
map<vector<pair<int,int> >,int> dp[50][50][4];
int dfs(int x,int y,int k,vector<pair<int,int> > vis){
	if( k < 0 ) return -1e9;
	vector< pair<int,int> > p;
	for(int i = 0 ; i < vis.size() ; i++){
		if( max(0,x-vis[i].first)+max(0,y-vis[i].second) <= k )
			p.push_back(vis[i]);
	}
	sort(p.begin(),p.end());
	vis = p;
	if( dp[x][y][k].count(vis) ) return dp[x][y][k][vis];
	
	int ans = ( x==W-1&&y==H-1 ? 0 : -1e9 );
	for(int i = 0 ;i < 4 ; i++){
		int tx = x + dx[i];
		int ty = y + dy[i];
		if( tx < 0 || tx >= W || ty < 0 || ty >= H || c[ty][tx] == '#') continue;
		int cst = 0;
		vector<pair<int,int> > t = vis;
		if( !binary_search(vis.begin(),vis.end(),make_pair(tx,ty)) ){
			cst = c[ty][tx] - '0';
			t.push_back(make_pair(tx,ty));
		}
		ans = max( ans , dfs(tx,ty,k-(i<2),t)+cst );
	}
	//cout << x << " " << y << " " << k << " " << vis.size() << endl;
	return dp[x][y][k][vis] = ans;
}
int main(){
	int K;
	cin >> H >> W >> K;
	for(int i = 0 ; i < H ; i++){
		for(int j = 0 ; j < W ; j++){
			cin >> c[i][j];
			if( c[i][j] == '.' ) c[i][j] = '0';
		}
	}
	cout << dfs(0,0,K,vector<pair<int,int> >(0)) << endl;
}