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 <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define mod 1000000007
#define INF 1000000000
#define LLINF 2000000000000000000LL

#define SIZE 500000

#define MAX_H 100
#define MAX_W 100

int H,W,K;
char mm[50][51];

int memo[50][50][1<<12][4];

int mo[5]={0,-1,0,1,0};

int dfs(int y,int x,int visit,int k,int h=0){
    int ret = -INF;
    bool visit_mm[50][50] = {0};
    
    //cerr << "(" << y << "," << x << ")" << endl;
    
    if(memo[y][x][visit][k] != 0) return memo[y][x][visit][k];
    
    if(y==H-1 && x==W-1 && k==0) return 0;
    
    int after_visit = (visit%(1<<10));
    int after_x = x,after_y = y;
    int calc_visit = visit;
    
    vector<pair<int,int> > vec;
    
    for(int i=0;i<min(h,6);i++){
        after_x -= mo[calc_visit%4];
        after_y -= mo[calc_visit%4+1];
        calc_visit/=4;
        visit_mm[after_y][after_x] = true;
        
        //cerr << "<- (" << after_y << "," << after_x << ")" << endl;
    }
    
    int add = 1;
    
    for(int i=0;i<4;i++){
        if(k>=1 || i>=2){
            int to_x = x+mo[i];
            int to_y = y+mo[i+1];
            
            if(to_x<0 || W<=to_x || to_y<0 || H<=to_y || mm[to_y][to_x]=='#') continue;
            
            if(visit_mm[to_y][to_x]) add = 0;
            else add = mm[to_y][to_x]-'0';
            
            ret = max(ret,dfs(to_y,to_x,after_visit*4+i,k-(i<=1),h+1)+add);
        }
    }
    
    return memo[y][x][visit][k] = ret;
    
}

int main(){
    
    scanf("%d%d%d",&H,&W,&K);
    
    for(int i=0;i<H;i++){
        scanf("%s",mm[i]);
    }
    
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            if(mm[i][j]=='.') mm[i][j] = '0';
        }
    }
    
    printf("%d\n",dfs(0,0,0,K));
    
    return 0;
}