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 <string>
#include <vector>
#include <iostream>
#include <algorithm>

#define INF -(1 << 23)

using namespace std;

const vector<int> dx = { 0, 1, 0, -1 };
const vector<int> dy = { -1, 0, 1, 0 };

int main()
{
	int H, W, K;

	scanf("%d", &H);
	scanf("%d", &W);
	scanf("%d", &K);

	vector<string> M(H);

	for (int i = 0; i < H; i++)
	{
		cin >> M[i];
	}

	for (int i = 0; i < H; i++)
	{
		for (int j = 0; j < W; j++)
		{
			if (M[i][j] == '.')
			{
				M[i][j] = '0';
			}
		}
	}

	vector<vector<vector<vector<int> > > > dp(H, vector<vector<vector<int> > >(W, vector<vector<int> >(K + 1, vector<int>(4096, INF))));

	dp[0][0][0][0] = 0;

	for (int rep = 0; rep <= K; rep++)
	{
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				if (M[i][j] != '#')
				{
					for (int k = 0; k <= K; k++)
					{
						for (int l = 0; l < 4096; l++)
						{
							int dir = l % 4;

							int p6 = l / 1024;
							int p5 = (l / 256) % 4;
							int p4 = (l / 64) % 4;
							int p3 = (l / 16) % 4;
							int p2 = (l / 4) % 4;
							int p1 = l % 4;

							int px1 = j + dx[p1];
							int px2 = px1 + dx[p2];
							int px3 = px2 + dx[p3];
							int px4 = px3 + dx[p4];
							int px5 = px4 + dx[p5];
							int px6 = px5 + dx[p6];
							int py1 = i + dy[p1];
							int py2 = py1 + dy[p2];
							int py3 = py2 + dy[p3];
							int py4 = py3 + dy[p4];
							int py5 = py4 + dy[p5];
							int py6 = py5 + dy[p6];

							bool ok1 = true, ok2 = true, ok3 = true, ok4 = true, ok5 = true, ok6 = true;

							if (0 <= px1 && px1 < W && 0 <= py1 && py1 < H)
							{
								if (M[py1][px1] > 48)
								{
									ok1 = false;
								}
							}

							if (0 <= px2 && px2 < W && 0 <= py2 && py2 < H)
							{
								if (M[py2][px2] > 48)
								{
									ok2 = false;
								}
							}

							if (0 <= px3 && px3 < W && 0 <= py3 && py3 < H)
							{
								if (M[py3][px3] > 48)
								{
									ok3 = false;
								}
							}

							if (0 <= px4 && px4 < W && 0 <= py4 && py4 < H)
							{
								if (M[py4][px4] > 48)
								{
									ok4 = false;
								}
							}

							if (0 <= px5 && px5 < W && 0 <= py5 && py5 < H)
							{
								if (M[py5][px5] > 48)
								{
									ok5 = false;
								}
							}

							if (0 <= px6 && px6 < W && 0 <= py6 && py6 < H)
							{
								if (M[py6][px6] > 48)
								{
									ok6 = false;
								}
							}

							int rate = 1;

							if ((j == px1 && i == py1 && !ok1) || (j == px2 && i == py2 && !ok2) || (j == px3 && i == py3 && !ok3) || (j == px4 && i == py4 && !ok4) || (j == px5 && i == py5 && !ok5) || (j == px6 && i == py6 && !ok6))
							{
								rate = 0;
							}

							if ((0 <= j + dx[dir] && j + dx[dir] < W && 0 <= i + dy[dir] && i + dy[dir] < H) && !((dir % 3) && k == 0))
							{
								for (int m = 0; m < 4; m++)
								{
									int r = m * 1024 + l / 4;

									if (dir % 3)
									{
										dp[i][j][k][l] = max(dp[i][j][k][l], dp[i + dy[dir]][j + dx[dir]][k - 1][r] + (M[i][j] - 48) * rate);
									}
									else
									{
										dp[i][j][k][l] = max(dp[i][j][k][l], dp[i + dy[dir]][j + dx[dir]][k][r] + (M[i][j] - 48) * rate);
									}
								}
							}
						}
					}
				}
			}
		}
	}

	int ret = -1;

	for (int i = 0; i <= K; i++)
	{
		for (int j = 0; j < 4096; j++)
		{
			ret = max(ret, dp[H - 1][W - 1][i][j]);
		}
	}

	printf("%d\n", ret);

	return 0;
}