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<bits/stdc++.h>
using namespace std;
int H,W,K;
int fld[50][50];
int memo[50][50][4][1<<12];
const int dy[]={0,1,-1,0};
const int dx[]={1,0,0,-1};
inline bool out(int y,int x){
    return y<0||y>=H||x<0||x>=W;
}

int dfs(int y,int x,int cnt,int S){
    if(memo[y][x][cnt][S]!=-1)return memo[y][x][cnt][S];

    if(y==H-1&&x==W-1&&cnt==K){
        return 0;
    }
    int ret=INT_MIN;
    int ty=y,tx=x;
    bool flag=true;
    for(int i=0;i<6;i++){
        int cur=(S>>(i*2))%4;
        ty-=dy[cur];
        tx-=dx[cur];
        if(out(ty,tx))break;
        if(ty==y&&tx==x)flag=false;
    }
    for(int i=0;i<4;i++){
        if(i>=2&&cnt==K)continue;
        ty=y+dy[i];
        tx=x+dx[i];
        if(out(ty,tx))continue;
        if(fld[ty][tx]==-1)continue;
        int tS=(S<<2)%(1<<(K*4));
        ret=max(ret,dfs(ty,tx,cnt+(i>=2?1:0),tS|i));
    }

    if(flag)ret+=fld[y][x];
    return memo[y][x][cnt][S]=ret;
}
int main(){
    scanf("%d%d%d",&H,&W,&K);
    for(int i=0;i<H;i++){
        char str[51];
        scanf("%s",str);
        for(int j=0;j<W;j++){
            int val;
            if(str[j]=='#')val=-1;
            else if(str[j]=='.')val=0;
            else val=str[j]-'0';
            fld[i][j]=val;
        }
    }
    fill_n(***memo,50*50*4*(1<<12),-1);
    printf("%d\n",dfs(0,0,0,0));

    return 0;
}