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 <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};

class Data
{
public:
    int y, x, b;
    vector<int> history;
    Data(int y0, int x0, int b0, int k)
    {
        y = y0;
        x = x0;
        b = b0;
        history.clear();
        for(int i=0; i<5; ++i){
            history.push_back(k % 4);
            k /= 4;
        }
    }
    bool move(int d)
    {
        int y2 = y;
        int x2 = x;
        y += dy[d];
        x += dx[d];
        b -= d / 2;

        bool ret = false;
        for(int i=4; i>=0; --i){
            y2 -= dy[history[i]];
            x2 -= dx[history[i]];
            if(y2 == y && x2 == x)
                ret = true;
        }

        history.erase(history.begin());
        history.push_back(d);

        return ret;
    }
    int toInt(){
        int ret = 0;
        for(int i=4; i>=0; --i){
            ret *= 4;
            ret += history[i];
        }
        return y + 50 * (x + 50 * (b + 4 * ret));
    }
};

int main()
{
    int h, w, n;
    cin >> h >> w >> n;

    vector<string> s(h);
    for(int i=0; i<h; ++i)
        cin >> s[i];

    vector<int> dp(50*50*4*1024, -1);
    dp[Data(0, 0, n, 0).toInt()] = 0;

    for(int b=n; b>=0; --b){
        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                for(int k=0; k<1024; ++k){
                    Data d(y, x, b, k);
                    int from = d.toInt();
                    if(dp[from] == -1)
                        continue;

                    for(int i=0; i<4; ++i){
                        Data d2 = d;
                        bool use = d2.move(i);
                        if(d2.y < 0 || d2.y >= h || d2.x < 0 || d2.x >= w || s[d2.y][d2.x] == '#' || d2.b < 0)
                            continue;

                        int next = dp[from];
                        if(!use && isdigit(s[d2.y][d2.x]))
                            next += s[d2.y][d2.x] - '0';

                        int to = d2.toInt();
                        dp[to] = max(dp[to], next);
                    }
                }
            }
        }
    }

    int ret = 0;
    for(int i=0; i<=n; ++i){
        for(int j=0; j<1024; ++j){
            ret = max(ret, dp[Data(h-1, w-1, i, j).toInt()]);
        }
    }
    cout << ret << endl;

    return 0;
}