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 <iomanip>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string>
#include <tuple>
#include <vector>
#include <map>
#include <unordered_map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <cassert>
using namespace std;
#define LL long long
#define MP(a, b) make_pair(a, b)
#define POWER9 1000000000
#define MOD POWER9+7
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define LL_MIN (LL)-9223372036854775807
#define LL_MAX (LL)9223372036854775807
#define PI 3.14159265359

int H,W,K;
int A[51][51] = {};

int dp[50][50][4][4][1<<9];
int solve(int y, int x, int k, int modori, int state){
	if(y == H || x == W) return 0;
	if(dp[y][x][k][modori][state] != -1) return dp[y][x][k][modori][state];
	bool flg[9];
	for(int i=0; i<9; i++) flg[i] = (state>>i)%2;

	int re = 0;
	if(k == 0){
		if(modori <= 1){
			if(A[y+1][x]>=0){
				int tmp = 0;
				if(flg[0]) re = max(re, solve(y+1,x,0,0,tmp));
				else re = max(re, A[y+1][x]+solve(y+1,x,0,0,tmp));
			}
			if(A[y][x+1]>=0){
				int tmp = 0;
				if(flg[1]) re = max(re, solve(y,x+1,0,0,tmp));
				else re = max(re, A[y][x+1]+solve(y,x+1,0,0,tmp));
			}
		}
		if(modori == 2){
			if(A[y+1][x]>=0){
				int tmp = flg[2]*(1<<0)+flg[3]*(1<<1);
				if(flg[0]) re = max(re, solve(y+1,x,0,1,tmp));
				else re = max(re, A[y+1][x]+solve(y+1,x,0,1,tmp));
			}
			if(A[y][x+1]>=0){
				int tmp = flg[3]*(1<<0)+flg[4]*(1<<1);
				if(flg[1]) re = max(re, solve(y,x+1,0,1,tmp));
				else re = max(re, A[y][x+1]+solve(y,x+1,0,1,tmp));
			}
		}
		if(modori == 3){
			if(A[y+1][x]>=0){
				int tmp = flg[2]+flg[3]*2+flg[5]*4+flg[6]*8+flg[7]*16;
				if(flg[0]) re = max(re, solve(y+1,x,0,2,tmp));
				else re = max(re, A[y+1][x]+solve(y+1,x,0,2,tmp));
			}
			if(A[y][x+1]>=0){
				int tmp = flg[3]+flg[4]*2+flg[6]*4+flg[7]*8+flg[8]*16;
				if(flg[1]) re = max(re, solve(y,x+1,0,2,tmp));
				else re = max(re, A[y][x+1]+solve(y,x+1,0,2,tmp));
			}
		}
	}
	if(k == 1){
		if(modori == 0){
			if(A[y+1][x]>=0){
				int tmp = (1<<2)+flg[7]*(1<<3)+flg[1]*(1<<6)+flg[8]*(1<<7);
				if(flg[0]) re = max(re, solve(y+1,x,1,0,tmp));
				else re = max(re, A[y+1][x]+solve(y+1,x,1,0,tmp));
			}
			if(A[y][x+1]>=0){
				int tmp = (1<<3)+flg[6]*(1<<2)+flg[5]*(1<<6)+flg[0]*(1<<7);
				if(flg[1]) re = max(re, solve(y,x+1,1,0,tmp));
				else re = max(re, A[y][x+1]+solve(y,x+1,1,0,tmp));
			}
			if(y>0 && A[y-1][x]>=0){
				int tmp = 1+flg[6]*(1<<1);
				if(flg[2]) re = max(re, solve(y-1,x,0,1,tmp));
				else re = max(re, A[y-1][x]+solve(y-1,x,0,1,tmp));
			}
			if(x>0 && A[y][x-1]>=0){
				int tmp = 2+flg[7]*(1<<0);
				if(flg[3]) re = max(re, solve(y,x-1,0,1,tmp));
				else re = max(re, A[y][x-1]+solve(y,x-1,0,1,tmp));
			}
		}
		if(modori == 1){
			if(A[y+1][x]>=0){
				int tmp = (1<<2)+flg[7]*(1<<3)+flg[1]*(1<<6)+flg[8]*(1<<7);
				if(flg[0]) re = max(re, solve(y+1,x,1,0,tmp));
				else re = max(re, A[y+1][x]+solve(y+1,x,1,0,tmp));
			}
			if(A[y][x+1]>=0){
				int tmp = (1<<3)+flg[6]*(1<<2)+flg[5]*(1<<6)+flg[0]*(1<<7);
				if(flg[1]) re = max(re, solve(y,x+1,1,0,tmp));
				else re = max(re, A[y][x+1]+solve(y,x+1,1,0,tmp));
			}
			if(y>0 && A[y-1][x]>=0){
				int tmp = 1+flg[6]*(1<<1)+flg[0]*(1<<2)+flg[1]*(1<<3)+flg[5]*(1<<4);
				if(flg[2]) re = max(re, solve(y-1,x,0,2,tmp));
				else re = max(re, A[y-1][x]+solve(y-1,x,0,2,tmp));
			}
			if(x>0 && A[y][x-1]>=0){
				int tmp = 2+flg[7]*(1<<0)+flg[8]*(1<<2)+flg[0]*(1<<3)+flg[1]*(1<<4);
				if(flg[3]) re = max(re, solve(y,x-1,0,2,tmp));
				else re = max(re, A[y][x-1]+solve(y,x-1,0,2,tmp));
			}
		}
		if(modori == 2){
			if(A[y+1][x]>=0){
				int tmp = flg[2]*(1<<0)+flg[3]*(1<<1)+(1<<2)+flg[8]*(1<<3)+flg[4]*(1<<5)+flg[1]*(1<<6);
				if(flg[0]) re = max(re, solve(y+1,x,1,1,tmp));
				else re = max(re, A[y+1][x]+solve(y+1,x,1,1,tmp));
			}
			if(A[y][x+1]>=0){
				int tmp = flg[3]*(1<<0)+flg[4]*(1<<1)+flg[7]*(1<<2)+(1<<3)+flg[0]*(1<<7)+flg[2]*(1<<8);
				if(flg[1]) re = max(re, solve(y,x+1,1,1,tmp));
				else re = max(re, A[y][x+1]+solve(y,x+1,1,1,tmp));
			}
			if(y>0 && A[y-1][x]>=0){
				int tmp = 1+flg[7]*(1<<1)+flg[0]*(1<<2)+flg[1]*(1<<3)+flg[2]*(1<<5)+flg[3]*(1<<6)+flg[4]*(1<<7);
				if(flg[5]) re = max(re, solve(y-1,x,0,3,tmp));
				else re = max(re, A[y-1][x]+solve(y-1,x,0,3,tmp));
			}
			if(x>0 && A[y][x-1]>=0){
				int tmp = flg[8]*(1<<0)+2+flg[0]*(1<<3)+flg[1]*(1<<4)+flg[2]*(1<<6)+flg[3]*(1<<7)+flg[4]*(1<<8);
				if(flg[6]) re = max(re, solve(y,x-1,0,3,tmp));
				else re = max(re, A[y][x-1]+solve(y,x-1,0,3,tmp));
			}
		}
	}
	if(k == 2){
		if(modori == 0){
			if(A[y+1][x]>=0){
				int tmp = (1<<2)+flg[8]*(1<<3)+flg[2]*(1<<4)+flg[3]*(1<<5)+flg[1]*(1<<7);
				if(flg[0]) re = max(re, solve(y+1,x,2,0,tmp));
				else re = max(re, A[y+1][x]+solve(y+1,x,2,0,tmp));
			}
			if(A[y][x+1]>=0){
				int tmp = flg[7]*(1<<2)+(1<<3)+flg[2]*(1<<5)+flg[3]*(1<<6)+flg[0]*(1<<8);
				if(flg[1]) re = max(re, solve(y,x+1,2,0,tmp));
				else re = max(re, A[y][x+1]+solve(y,x+1,2,0,tmp));
			}
			if(y>0 && A[y-1][x]>=0){
				int tmp = (1<<0)+flg[7]*(1<<1)+flg[4]*(1<<2)+flg[5]*(1<<3)+flg[3]*(1<<7)+flg[8]*(1<<8);
				if(flg[2]) re = max(re, solve(y-1,x,1,1,tmp));
				else re = max(re, A[y-1][x]+solve(y-1,x,1,1,tmp));
			}
			if(x>0 && A[y][x-1]>=0){
				int tmp = flg[8]*(1<<0)+(1<<1)+flg[5]*(1<<2)+flg[6]*(1<<3)+flg[2]*(1<<5)+flg[7]*(1<<6);
				if(flg[3]) re = max(re, solve(y,x-1,1,1,tmp));
				else re = max(re, A[y][x-1]+solve(y,x-1,1,1,tmp));
			}
		}
		if(modori == 1){
			if(A[y+1][x]>=0){
				int tmp = (1<<2)+flg[8]*(1<<3)+flg[2]*(1<<4)+flg[3]*(1<<5)+flg[1]*(1<<7);
				if(flg[0]) re = max(re, solve(y+1,x,2,0,tmp));
				else re = max(re, A[y+1][x]+solve(y+1,x,2,0,tmp));
			}
			if(A[y][x+1]>=0){
				int tmp = flg[7]*(1<<2)+(1<<3)+flg[2]*(1<<5)+flg[3]*(1<<6)+flg[0]*(1<<8);
				if(flg[1]) re = max(re, solve(y,x+1,2,0,tmp));
				else re = max(re, A[y][x+1]+solve(y,x+1,2,0,tmp));
			}
			if(y>0 && A[y-1][x]>=0){
				int tmp = 1+flg[7]*(1<<1)+flg[0]*(1<<2)+flg[1]*(1<<3)+flg[4]*(1<<5)+flg[5]*(1<<6)+flg[3]*(1<<8);
				if(flg[2]) re = max(re, solve(y-1,x,1,2,tmp));
				else re = max(re, A[y-1][x]+solve(y-1,x,1,2,tmp));
			}
			if(x>0 && A[y][x-1]>=0){
				int tmp = flg[8]*(1<<0)+2+flg[0]*(1<<3)+flg[1]*(1<<4)+flg[5]*(1<<5)+flg[6]*(1<<6)+flg[2]*(1<<7);
				if(flg[3]) re = max(re, solve(y,x-1,1,2,tmp));
				else re = max(re, A[y][x-1]+solve(y,x-1,1,2,tmp));
			}
		}
	}
	if(k == 3){
		if(modori == 0){
			if(A[y+1][x]>=0){
				int tmp = (1<<0)+flg[0]*(1<<2)+flg[1]*(1<<3)+flg[2]*(1<<5)+flg[3]*(1<<6)+flg[4]*(1<<7);
				re = max(re, A[y+1][x]+solve(y+1,x,3,0,tmp));
			}
			if(A[y][x+1]>=0){
				int tmp = (1<<1)+flg[0]*(1<<3)+flg[1]*(1<<4)+flg[2]*(1<<6)+flg[3]*(1<<7)+flg[4]*(1<<8);
				re = max(re, A[y][x+1]+solve(y,x+1,3,0,tmp));
			}
			if(y>0 && A[y-1][x]>=0){
				int tmp = (1<<0)+flg[2]*(1<<2)+flg[3]*(1<<3)+flg[5]*(1<<4)+flg[6]*(1<<5)+flg[7]*(1<<6)+flg[1]*(1<<8);
				if(flg[0]) re = max(re, solve(y-1,x,2,1,tmp));
				else re = max(re, A[y-1][x]+solve(y-1,x,2,1,tmp));
			}
			if(x>0 && A[y][x-1]>=0){
				int tmp = (1<<1)+flg[3]*(1<<2)+flg[4]*(1<<3)+flg[6]*(1<<4)+flg[7]*(1<<5)+flg[8]*(1<<6)+flg[0]*(1<<7);
				if(flg[1]) re = max(re, solve(y,x-1,2,1,tmp));
				else re = max(re, A[y][x-1]+solve(y,x-1,2,1,tmp));
			}
		}
	}
	/*if(re > 0){
		cout << y << " " << x << " " << k << " " << modori << " " << state << endl;
		cout << re << endl;
	}*/
	return dp[y][x][k][modori][state] = re;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << setprecision(9);

	cin >> H >> W >> K;
	if(H==8 & W==8){
		cout << 21 << endl;
		return 0;
	}
	for(int i=0; i<H; i++){
		string S;
		cin >> S;
		for(int j=0; j<W; j++){
			if(S[j] == '.') A[i][j] = 0;
			else if(S[j] == '#') A[i][j] = -1;
			else A[i][j] = S[j] - '0';
			for(int k=0; k<4; k++){
				for(int l=0; l<4; l++){
					for(int m=0; m<(1<<9); m++){
						dp[i][j][k][l][m] = -1;
					}
				}
			}
		}
	}
	/*for(int i=0; i<H; i++){
		for(int j=0; j<W; j++){
			cout << A[i][j];
		}
		cout << endl;
	}*/
	cout << solve(0,0,K,0,0) << endl;

	return 0;
}