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 <vector>
#include <string>
#include <algorithm>
#include <map>

using namespace std;
typedef pair<int, int> p;

int k;
vector<string> ban;

void move(int i, int j, int time, map<vector<p>, int> &before, map<vector<p>, int> &after, bool turn) {
    for (auto back:before) {
        vector<p> now;
        bool shouldAdd = '0' <= ban[i][j] && ban[i][j] <= '9';
        for (auto place:back.first) {
            if (turn || place.first + place.second >= i + j - (k - time))now.push_back(place);
            if (place.first == i && place.second == j)shouldAdd = false;
        }
        if (shouldAdd)now.push_back(p(i, j));

        int cost = back.second + (shouldAdd ? ban[i][j] - '0' : 0);
        if (after.find(now) == after.end() || after[now] < cost) after[now] = cost;
    }
}

int main() {
    int h, w;
    cin >> h >> w >> k;
    ban = vector<string>(h);
    for (int i = 0; i < h; i++)cin >> ban[i];

    vector<vector<map<vector<p>, int>>> dp(h, vector<map<vector<p>, int>>(w, map<vector<p>, int>()));
    dp[0][0][{}] = 0;

    for (int time = 0; time <= k; time++) {
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (ban[i][j] == '#')continue;

                if (i - 1 >= 0) { //down
                    move(i, j, time, dp[i - 1][j], dp[i][j], false);
                }
                if (j - 1 >= 0) { //right
                    move(i, j, time, dp[i][j - 1], dp[i][j], false);
                }
            }
        }

        if (time == k) break;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (ban[i][j] == '#')continue;

                if (i + 1 < h) { // up
                    move(i, j, time, dp[i + 1][j], dp[i][j], true);
                }
                if (j + 1 < w) { // left
                    move(i, j, time, dp[i][j + 1], dp[i][j], true);
                }
            }
        }
    }

    int ret = -1145141919;
    for (auto x:dp[h - 1][w - 1]) ret = max(ret, x.second);
    cout << ret << endl;
    return 0;
}