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

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

const std::vector<std::vector<std::pair<int, int>>>
    peris{
        {{0, 0},
         {1, 0},
         {2, 0},
         {3, 0},
         {0, 1},
         {1, 1},
         {2, 1},
         {0, 2},
         {1, 2},
         {0, 3}},

        {{0, -1},
         {1, -1},
         {2, -1},
         {-1, 0},
         {0, 0},
         {1, 0},
         {2, 0},
         {-1, 1},
         {0, 1},
         {1, 1},
         {-1, 2},
         {0, 2}},

        {{0, -2},
         {1, -2},
         {-1, -1},
         {0, -1},
         {1, -1},
         {-2, 0},
         {-1, 0},
         {0, 0},
         {1, 0},
         {-2, 1},
         {-1, 1},
         {0, 1}},

        {{0, -3},
         {-1, -2},
         {0, -2},
         {-2, -1},
         {-1, -1},
         {0, -1},
         {-3, 0},
         {-2, 0},
         {-1, 0},
         {0, 0}}};

const std::vector<int> ks{10, 12, 12, 10};
constexpr int INF = 1 << 30;

void solve() {
    int h, w, k;
    std::cin >> h >> w >> k;

    std::vector<std::string> ss(h);
    for (auto& s : ss) {
        std::cin >> s;
        for (auto& c : s) {
            if (c == '.') c = '0';
        }
    }

    auto dp = vec(k + 1, vec(1 << 12, vec(h, vec(w, -INF))));
    dp[k][0][0][0] = 0;

    for (int l = k; l >= 0; --l) {
        for (int x = 0; x < h; ++x) {
            for (int y = 0; y < w; ++y) {
                if (ss[x][y] == '#') continue;

                for (int prev = 0; prev < (1 << ks[l]); ++prev) {
                    for (int i = 1; i <= 2; ++i) {
                        int dx = (i & 1),
                            dy = (i >> 1);

                        int nx = x + dx,
                            ny = y + dy;
                        if (nx >= h || ny >= w) continue;

                        int score = dp[l][prev][x][y] + (ss[x][y] - '0');
                        int nprev = prev;
                        for (int j = 0; j < ks[l]; ++j) {
                            if (peris[l][j] != std::make_pair(0, 0)) continue;
                            if ((prev >> j) & 1) score -= (ss[x][y] - '0');
                            nprev |= (1 << j);
                        }

                        int next = 0;
                        for (int j = 0; j < ks[l]; ++j) {
                            auto p = peris[l][j];
                            p.first += dx, p.second += dy;

                            for (int pj = 0; pj < ks[l]; ++pj) {
                                if (p == peris[l][pj]) {
                                    int b = (nprev >> pj) & 1;
                                    next |= (b << j);
                                    break;
                                }
                            }
                        }

                        dp[l][next][nx][ny] = std::max(dp[l][next][nx][ny], score);
                    }

                    if (l == 0) continue;

                    for (int i = 1; i <= 2; ++i) {
                        int dx = (i & 1),
                            dy = (i >> 1);

                        int nx = x - dx,
                            ny = y - dy;
                        if (nx < 0 || ny < 0) continue;

                        int score = dp[l][prev][x][y] + (ss[x][y] - '0');
                        int nprev = prev;
                        for (int j = 0; j < ks[l]; ++j) {
                            if (peris[l][j] != std::make_pair(0, 0)) continue;
                            if ((prev >> j) & 1) score -= (ss[x][y] - '0');
                            nprev |= (1 << j);
                        }

                        int nl = l - 1;
                        int next = 0;
                        for (int j = 0; j < ks[nl]; ++j) {
                            auto p = peris[nl][j];
                            p.first -= dx, p.second -= dy;

                            for (int pj = 0; pj < ks[l]; ++pj) {
                                if (p == peris[l][pj]) {
                                    int b = (nprev >> pj) & 1;
                                    next |= (b << j);
                                    break;
                                }
                            }
                        }

                        dp[nl][next][nx][ny] = std::max(dp[nl][next][nx][ny], score);
                    }
                }
            }
        }
    }

    int ans = 0;
    for (int l = 0; l <= k; ++l) {
        for (int p = 0; p < (1 << ks[l]); ++p) {
            ans = std::max(ans, dp[l][p][h - 1][w - 1]);
        }
    }

    std::cout << ans << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}