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 <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int dp[55][55][4][1<<12];
char f[55][55];
int w,h,K;
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
bool out(int x,int y)
{
	if(x<0||x>=h||y<0||y>=w)return true;
	return false;
}
bool revisit(int bit)
{
	int tx=0,ty=0;
	for(int i=0;i<6;i++,bit>>=2)
	{
		int id=bit%4;
		tx+=dx[id];ty+=dy[id];
		if(tx==0&&ty==0)return true;
	}
	return false;
}
int rec(int x,int y,int k,int bit)
{
	if(x==0&&y==0)return 0;
	if(dp[x][y][k][bit]!=-INF)return dp[x][y][k][bit];
	int num=0;
	if(f[x][y]!='.'&&f[x][y]!='#'&&!revisit(bit))num=f[x][y]-'0';
	int res=-INF;
	for(int i=0;i<4;i++)
	{
		int nx=x+dx[i],ny=y+dy[i];
		if(!out(nx,ny)&&f[x][y]!='#'&&k-(i>=2)>=0)
		{
			res=max(res,rec(nx,ny,k-(i>=2),((1<<12)-1)&((bit<<2)+i))+num);
		}
	}
	return dp[x][y][k][bit]=res;
}
int main()
{
	cin >> h >> w >> K;
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			cin >> f[i][j];
		}
	}
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			for(int k=0;k<4;k++)
			{
				for(int l=0;l<(1<<12);l++)
				{
					dp[i][j][k][l]=-INF;
				}
			}
		}
	}
	cout << rec(h-1,w-1,K,0) << endl;
	return 0;
}