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<algorithm>
#include<vector>
#include<cstdio>
#include<list>
#include<queue>
#include<string.h>
#include<functional>
#include<stack>
#include<deque>
#include<string>
#include<limits.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
using namespace std;

string e[50];
int dp[50][50][4][1 << 11];//??¨??£??????1
int d[4] = { 9,11,11,9 };
int dx[4][11] = {
	{-3,-2,-2,-1,-1,-1,0,0,0},{-2,-2,-1,-1,-1,0,0,0,1,1,1},{-1,-1,-1,0,0,0,1,1,1,2,2},{0,0,0,1,1,1,2,2,3}
};
int dy[4][11] = {
	{0,-1,0,-2,-1,0,-3,-2,-1},{0,1,-1,0,1,-2,-1,1,-2,-1,0},{0,1,2,-1,1,2,-1,0,1,-1,0},{1,2,3,0,1,2,0,1,0}
};
bool used[50][50];
signed main() {
	int a, b, c; cin >> a >> b >> c;
	for (int i = 0; i < a; i++)cin >> e[i];
	memset(dp, -1, sizeof(dp));
	if (e[0][1] >= '1'&&e[0][1] <= '9') {
		dp[0][1][0][0] = e[0][1] - '0';
	}
	else if (e[0][1] == '.') {
		dp[0][1][0][0] = 0;
	}
	if (e[1][0] >= '1'&&e[1][0] <= '9') {
		dp[1][0][0][0] = e[1][0] - '0';
	}
	else if (e[1][0] == '.') {
		dp[1][0][0][0] = 0;
	}
	for (int i = 0; i < a + b + c * 2 + 8; i++) {
		for (int j = 0; j < a; j++) {
			for (int k = 0; k <= c; k++) {
				int l = i - j - k*2;
				if (l<0 || l>=b)continue;
				//??? j????¨? l ?????° k
				if (e[j][l] == '#')continue;
				for (int m = 0; m < (1 << d[k]); m++) {
					memset(used, false, sizeof(used));
					if (dp[j][l][k][m] == -1)continue;
					for (int r = 0; r < d[k]; r++) {
						if (m&(1 << r)) {
							used[j + dx[k][r]][l + dy[k][r]] = true;
						}
					}
					if (e[j][l] >= '1'&&e[j][l] <= '9') {
						used[j][l] = true;
					}
					//???
					if (j + 1 < a) {
						int sum = dp[j][l][k][m];
						if (e[j + 1][l] >= '1'&&e[j + 1][l] <= '9' && !used[j + 1][l]) {
							sum += e[j + 1][l] - '0';
						}
						int bit = 0;
						for (int i = 0; i < d[k]; i++) {
							int x = j + 1 + dx[k][i], y = l + dy[k][i];
							if (x >= 0 && x < a&&y >= 0 && y < b&&e[x][y] >= '1'&&e[x][y] <= '9'&&used[x][y]) {
								bit |= (1 << i);
							}
						}
						dp[j + 1][l][k][bit] = max(dp[j + 1][l][k][bit], sum);
					}
					//???
					if (l + 1 < b) {
						int sum = dp[j][l][k][m];
						if (e[j][l + 1] >= '1'&&e[j][l + 1] <= '9' && !used[j][l + 1]) {
							sum += e[j][l + 1] - '0';
						}
						int bit = 0;
						for (int i = 0; i < d[k]; i++) {
							int x = j + dx[k][i], y = l + 1 + dy[k][i];
							if (x >= 0 && x < a&&y >= 0 && y < b&&e[x][y] >= '1'&&e[x][y] <= '9' && used[x][y]) {
								bit |= 1 << i;
							}
						}
						dp[j][l + 1][k][bit] = max(dp[j][l + 1][k][bit], sum);
					}
					//???
					if (j - 1 >= 0&&k<c) {
						int sum = dp[j][l][k][m];
						if (e[j - 1][l] >= '1'&&e[j - 1][l] <= '9' && !used[j - 1][l]) {
							sum += e[j - 1][l] - '0';
						}
						int bit = 0;
						for (int i = 0; i < d[k+1]; i++) {
							int x = j - 1 + dx[k+1][i], y = l + dy[k+1][i];
							if (x >= 0 && x < a&&y >= 0 && y < b&&e[x][y] >= '1'&&e[x][y] <= '9' && used[x][y]) {
								bit |= 1 << i;
							}
						}
						dp[j - 1][l][k + 1][bit] = max(dp[j - 1][l][k + 1][bit], sum);
					}
					//???
					if (l - 1 >= 0&&k<c) {
						int sum = dp[j][l][k][m];
						if (e[j][l - 1] >= '1'&&e[j][l - 1] <= '9' && !used[j][l - 1]) {
							sum += e[j][l - 1] - '0';
						}
						int bit = 0;
						for (int i = 0; i < d[k+1]; i++) {
							int x = j + dx[k + 1][i], y = l - 1 + dy[k + 1][i];
							if (x >= 0 && x < a&&y >= 0 && y < b&&e[x][y] >= '1'&&e[x][y] <= '9' && used[x][y]) {
								bit |= 1 << i;
							}
						}
						dp[j][l - 1][k + 1][bit] = max(dp[j][l - 1][k + 1][bit], sum);
					}
				}
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < (1 << d[i]); j++) {
			ans = max(ans, dp[a - 1][b - 1][i][j]);
		}
	}
	cout << ans << endl;
}