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;

const int INF = 1 << 28;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
typedef pair<int, int> Point;

int H, W, K;
char grid[64][64];
vector<Point> rem[4];
int vis[64][64];
int dp[50][50][4][1 << 12];

void init()
{
	memset(grid, '#', sizeof(grid));
	memset(dp, -1, sizeof(dp));
	
	for (int k = 0; k <= 3; k++){
		for (int x = -k; x <= (3 - k); x++){
			for (int y = -k; y <= (3 - k); y++){
				//if (x == 0 && y == 0) continue;
				if (-k <= (x + y) && (x + y) <= (3 - k)){
					rem[k].push_back(Point(x, y));
				}
			}
		}
	}
}

int calcBit(int x, int y, int k)
{
	int res = 0;
	for (Point &p : rem[k]){
		res <<= 1;
		int X = x + p.first;
		int Y = y + p.second;
		if (vis[X][Y] > 0) res |= 1;
	}
	return res;
}

int calc(int x, int y, int k)
{
	int bit = calcBit(x, y, k);
	int &res = dp[x - 3][y - 3][k][bit];
	if (~res) return res;
	
	int add = 0;
	if (grid[x][y] >= '1' && grid[x][y] <= '9'){
		if (vis[x][y] == 0){
			add = grid[x][y] - '0';
		}
	}
	
	if (x == H + 2 && y == W + 2 && k == 0){
		return res = add;
	}
	
	res = -INF;
	
	vis[x][y]++;
	for (int dir = 0; dir < 4; dir++){
		int nx = x + dx[dir];
		int ny = y + dy[dir];
		int nk = k - (dir < 2);
		if (grid[nx][ny] == '#') continue;
		if (nk < 0) continue;
		res = max(res, calc(nx, ny, nk));
	}
	vis[x][y]--;
	
	return res += add;
}

int main(void)
{
	init();
	
	scanf("%d %d %d", &H, &W, &K);
	
	for (int i = 0; i < H; i++){
		for (int j = 0; j < W; j++){
			scanf(" %c", &grid[i + 3][j + 3]);
		}
	}
	
	printf("%d\n", calc(3, 3, K));
	
	return 0;
}