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<bitset>
#include<vector>
using namespace std;
int H, W, K;
char town[51][51];
int ans = 0;
int dp[52][52][4][5][5][1 << 10] = {};
int ac[5][5] = { { 10, 10, 10, 10, 10 },
{ 10, 3, 6, 8, 9 },
{ 10, 2, 5, 7, 10 },
{ 10, 1, 4, 10, 10 },
{ 10, 0, 10, 10, 10 }
};

int solve() {
	dp[1][1][0][1][1][1 << 3] = 1;
	for (int k = 0; k <= K; k++) {
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				for (int x = 1; x <= 4; x++) {
					for (int y = 1; y <= 4; y++) {
						for (int st = 0; st < (1 << 10); st++) {
							if (dp[i][j][k][x][y][st] <= 0) continue;
							bitset<11> bit(st);
							int b[11] = {};
							for (int q = 0; q < 10; q++) {
								if (bit[q]) b[q] = 1;
							}
							int to = 0;
							int res = dp[i][j][k][x][y][st];
							//east
							if (town[i][j + 1] != '#') {
								to |= b[1];
								to |= b[2] << 1;
								to |= b[3] << 2;
								to |= b[5] << 4;
								to |= b[6] << 5;
								to |= b[8] << 7;
								if (b[ac[x - 1][y]] == 0 && '1' <= town[i][j + 1] && town[i][j + 1] <= '9') res += town[i][j + 1] - '0';
								to |= 1 << ac[x][y];

								dp[i][j + 1][k][x][y][to] = max(dp[i][j + 1][k][x][y][to], res);
							}
							//south
							to = 0;
							res = dp[i][j][k][x][y][st];
							if (town[i + 1][j] != '#') {
								to |= b[1] << 4;
								to |= b[2] << 5;
								to |= b[3] << 6;
								to |= b[5] << 7;
								to |= b[6] << 8;
								to |= b[8] << 9;
								if (b[ac[x][y - 1]] == 0 && '1' <= town[i + 1][j] && town[i + 1][j] <= '9') res += town[i + 1][j] - '0';
								to |= 1 << ac[x][y];

								dp[i + 1][j][k][x][y][to] = max(dp[i + 1][j][k][x][y][to], res);
							}
							if (k < K) {
								//west
								res = dp[i][j][k][x][y][st];
								if (town[i][j - 1] != '#') {
									if (b[ac[x + 1][y]] == 0 && '1' <= town[i][j - 1] && town[i][j - 1] <= '9') res += town[i][j - 1] - '0';
									to = st | (1 << ac[x + 1][y]);
									dp[i][j - 1][k + 1][x + 1][y][to] = max(dp[i][j - 1][k + 1][x + 1][y][to], res);
								}
								//north
								res = dp[i][j][k][x][y][st];
								if (town[i - 1][j] != '#') {
									if (b[ac[x][y + 1]] == 0 && '1' <= town[i - 1][j] && town[i - 1][j] <= '9') res += town[i - 1][j] - '0';
									to = st | (1 << ac[x][y + 1]);
									dp[i - 1][j][k + 1][x][y + 1][to] = max(dp[i - 1][j][k + 1][x][y + 1][to], res);
								}
							}
						}
					}
				}
			}
		}
	}
	for (int k = 0; k <= K; k++) {
		for (int x = 1; x <= 4; x++) {
			for (int y = 1; y <= 4; y++) {
				for (int st = 0; st < (1 << 10); st++) {
					ans = max(ans, dp[H][W][k][x][y][st]);
				}
			}
		}
	}
	return ans - 1;
}

void input() {
	cin >> H >> W >> K;
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			cin >> town[i][j];
		}
	}
}

int main() {
	input();
	cout << solve() << endl;
	return 0;
}