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;

void solve();

int H,W,K;
int fid[60][60] = {0};
int dp[60][60][5][4][4][4][4][4];
int dx[] = {0,1,0,-1},dy[] = {1,0,-1,0};
int mem[] = {1,10,-1,-10};

int main()
{
    scanf("%d%d%d",&H,&W,&K);
    for(int i = 0; i < H; i++)
    {
        for(int j = 0; j < W; j++)
        {
            char C;
            scanf(" %c",&C);
            if('1' <= C <= '9')
            {
                fid[i][j] = C - '0';
            }
            if(C == '#')
            {
                fid[i][j] = -1;
            }
            if(C == '.')
            {
                fid[i][j] = 0;
            }
        }
    }
    for(int i = 0; i < H; i++)
    {
        for(int j = 0; j < W; j++)
        {
            for(int k = 0; k < K + 1; k++)
            {
                for(int a = 0; a < 4; a++)
                {
                    for(int b = 0; b < 4; b++)
                    {
                        for(int c = 0; c < 4; c++)
                        {
                            for(int d = 0; d < 4; d++)
                            {
                                for(int e = 0; e < 4; e++)
                                {
                                    dp[i][j][k][a][b][c][d][e] = -1;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    solve();
    return 0;
}

void solve()
{
    dp[0][0][0][0][0][0][0][0] = fid[0][0];
    for(int k = 0; k < K + 1; k++)
    {
        for(int i = 0; i < H; i++)
        {
            for(int j = 0; j < W; j++)
            {
                for(int a = 0; a < 4; a++)
                {
                    for(int b = 0; b < 4; b++)
                    {
                        for(int c = 0; c < 4; c++)
                        {
                            for(int d = 0; d < 4; d++)
                            {
                                for(int e = 0; e < 4; e++)
                                {
                                    if(dp[i][j][k][a][b][c][d][e] == -1)continue;
                                    for(int f = 0; f < 4; f++)
                                    {
                                        int X = i + dx[f],Y = j + dy[f];
                                        if(X < 0 || H <= X || Y < 0 || W <= Y || fid[X][Y] == -1)continue;
                                        if(f < 2)
                                        {
                                            if(!(mem[f] + mem[e]) || !(mem[f] + mem[e] + mem[d] + mem[c]) || !(mem[f] + mem[e] + mem[d] + mem[c] + mem[b] + mem[a]))
                                            {
                                                dp[X][Y][k][b][c][d][e][f] = max(dp[X][Y][k][b][c][d][e][f],dp[i][j][k][a][b][c][d][e]);
                                            }
                                            else
                                            {
                                                dp[X][Y][k][b][c][d][e][f] = max(dp[X][Y][k][b][c][d][e][f],dp[i][j][k][a][b][c][d][e] + fid[X][Y]);
                                            }
                                        }
                                        else
                                        {
                                            if(k == K)continue;
                                            if(!(mem[f] + mem[e]) || !(mem[f] + mem[e] + mem[d] + mem[c]) || !(mem[f] + mem[e] + mem[d] + mem[c] + mem[b] + mem[a]))
                                            {
                                                dp[X][Y][k + 1][b][c][d][e][f] = max(dp[X][Y][k + 1][b][c][d][e][f],dp[i][j][k][a][b][c][d][e]);
                                            }
                                            else
                                            {
                                                dp[X][Y][k + 1][b][c][d][e][f] = max(dp[X][Y][k + 1][b][c][d][e][f],dp[i][j][k][a][b][c][d][e] + fid[X][Y]);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    int ans = 0;
    for(int a = 0; a < 4; a++)
    {
        for(int b = 0; b < 4; b++)
        {
            for(int c = 0; c < 4; c++)
            {
                for(int d = 0; d < 4; d++)
                {
                    for(int e = 0; e < 4; e++)
                    {
                        ans = max(ans,dp[H - 1][W - 1][K][a][b][c][d][e]);
                    }
                }
            }
        }
    }
    printf("%d\n",ans);
    return;
}