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>
#define INF 0x3f3f3f3f
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;

int h, w, k;
char s[51][51];
int dp[51][51][1 << 12][4], dx[]{ 1,-1,0,0 }, dy[]{ 0,0,1,-1 };
bool used[51][51];
void mark(int x, int y, int t, int s) {
	used[x][y] = true;
	if (!s)return;
	int a = t % 4;
	x -= dx[a], y -= dy[a];
	t >>= 2;
	mark(x, y, t, s - 1);
}
int rec(int x, int y, int t, int a, int cnt) {
	if (dp[x][y][t][a] != -1)return dp[x][y][t][a];
	int res = -INF;
	rep(i, 4) {
		if (i % 2 && !a)continue;
		int nx = x + dx[i], ny = y + dy[i];
		if (nx < 0 || ny < 0 || nx >= h || ny >= w || s[nx][ny] == '#')continue;
		memset(used, 0, sizeof(used));
		mark(x, y, t, cnt);
		int v = ((!used[nx][ny] && '1' <= s[nx][ny] && s[nx][ny] <= '9') ? s[nx][ny] - '0' : 0);
		int p = (t >> (k * 4 - 2)) << (k * 4 - 2); p = t - p;
		p = (p << 2) + i; int b = cnt; if (b < 2 * k)b++;
		int o = rec(nx, ny, p, a - i % 2, b);
		if (o != -INF)res = max(res, o + v);
	}
	if (x == h - 1 && y == w - 1 && res == -INF)return dp[x][y][t][a] = 0;
	return dp[x][y][t][a] = res;
}
int main() {
	scanf("%d%d%d", &h, &w, &k);
	rep(i, h)scanf("%s", s[i]);
	memset(dp, -1, sizeof(dp));
	printf("%d\n", rec(0, 0, 0, k, 0));
}