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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <cassert>
using namespace std;

int h, w, k;
char board[100][100];

int getboard(int i, int j)
{
	if (i < 0 || j < 0 || i >= h || j >= w)
	{
		return -1;
	}
	return (board[i][j] == '#') ? -1 : ((board[i][j] == '.') ? 0 : (board[i][j] - '0'));
}

const int PMAX = 1 << 12;
int memo[50][50][4][PMAX];
const int hsy[4] = { 1,0,-1,0 };
const int hsx[4] = { 0,1,0,-1 };
const int INF = 123456789;
int dp(int i, int j, int x, int path)
{
	if (x > k || x < 0)return -INF;
	if (i < 0 || j < 0 || i >= h || j >= w)return -INF;
	if (memo[i][j][x][path] != -1)return memo[i][j][x][path];
	int ans = -INF;
	int kk = x;
	if (!(getboard(i, j) < 0))
	{
		for (int d = 0; d < 4; ++d)
		{
			if (d >= 2)
			{
				kk = x - 1;
				if (kk < 0)continue;
			}
			int d2 = d << 10;
			int idp = (path >> 2) % (1 << 10) + d2;
			int cans = dp(i - hsy[path % 4], j - hsx[path % 4], kk, idp);
			int u = hsy[path % 4], v = hsx[path % 4];
			bool flag = false;
			for (int bit = 0; bit < 6; ++bit)
			{
				u += hsy[idp % 4];
				v += hsx[idp % 4];
				idp /= 4;
				if (u == 0 && v == 0)flag = true;
			}
			if (!flag)cans += getboard(i, j);
			ans = max(ans, cans);
		}
	}
	return memo[i][j][x][path] = ans;
}

int main(void)
{
	scanf("%d %d %d", &h, &w, &k);
	for (int i = 0; i < h; ++i)scanf("%s", board[i]);
	memset(memo, 0xFF, sizeof(memo));
	memo[0][0][0][0] = 0;
	int ans = 0;
	for (int x = 0; x <= k; ++x)
	{
		for (int p = 0; p < PMAX; ++p)
		{
			int p2 = p;
			int bcount = 0;
			for (int bit = 0; bit < 6; ++bit)
			{
				if (p2 % 4 >= 2)++bcount;
				p2 /= 4;
			}
			if (bcount > k - x)continue;
			ans = max(ans, dp(h - 1, w - 1, x, p));
		}
	}
	printf("%d\n", ans);
	return 0;
}