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;

//#define int long long
#define PB push_back
#define MK make_pair
#define MKT make_tuple
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, pii> pipi;

//constexpr int INF = 1LL<<60;
constexpr int INF = 1 << 28;
//constexpr int MOD = 100000;
constexpr int MAX_N = 52;
constexpr int dx[] = { 1, 0, -1, 0 };
constexpr int dy[] = { 0, 1, 0, -1 };

int mem[MAX_N][MAX_N][4][1 << 6][1 << 6], h, w, k, fld[MAX_N][MAX_N];

inline bool Chk(int x, int y, int mask1, int mask2) {
    constexpr int len = 6;
    int cx = x, cy = y;
    for (int i = 0; i < len; ++i) {
        int b1 = (mask1 & (1 << (len - 1 - i))) ? 1 : 0, b2 = (mask2 & (1 << (len - 1 - i))) ? 1 : 0;
        int idx = b1 + 2 * b2;
        cx -= dx[idx];
        cy -= dy[idx];
        if (x == cx && y == cy) {
            return true;
        }
    }
    return false;
}

int Solve(int x, int y, int c, int mask1, int mask2) {
    if (x == w + 1 && y == h) return 0;
    if (mem[x][y][c][mask1][mask2] != -1) return mem[x][y][c][mask1][mask2];

    int cur = Chk(x, y, mask1, mask2) ? 0 : fld[x][y];
    int res = 0;
    bool flg = false;
    for (int i = 0; i < 4; ++i) {
        if (1 < i && k <= c) break;
        int tx = x + dx[i], ty = y + dy[i];
        int nc = (1 < i) ? c + 1 : c;
        if (fld[tx][ty] == -1) continue;

        int nm1 = (mask1 >> 1) + ((i % 2) << 5), nm2 = (mask2 >> 1) + ((1 < i) << 5);
        int tmp = Solve(tx, ty, nc, nm1, nm2);
        if (tmp <= -INF) {
            continue;
        }
        flg = true;
        res = max(res, tmp);
    }

    if (!flg) {
        res = -INF;
        cur = 0;
    }

    return mem[x][y][c][mask1][mask2] = res + cur;
}

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

    memset(fld, -1, sizeof(fld));
    cin >> h >> w >> k;
    for (int y = 1; y <= h; ++y) {
        string s;
        cin >> s;
        for (int x = 1; x <= w; ++x) {
            char c = s[x - 1];
            if (c == '#') {
                fld[x][y] = -1;
                continue;
            } else if (c == '.') {
                fld[x][y] = 0;
                continue;
            }
            fld[x][y] = c - '0';
        }
    }
    fld[w + 1][h] = 0;
    memset(mem, -1, sizeof(mem));
    cout << max(0, Solve(1, 1, 0, 0, 0)) << endl;
}