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 <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }


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

int ch[4][4][12] = {
    {
        {-1, -1, 0, -1, 1, 2, -1, 3, 4, 5, -1, -1}, 
        {-1, -1, 1, -1, 3, 4, -1, 6, 7, 8, -1, -1}, 
        {0, 2, 3, 5, 6, 7, -1, 9, 10, 11, -1, -1}, 
        {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1}
    },
    {
        {-1, -1, -1, 0, 1, -1, 2, 3, 4, 5, 6, 7}, 
        {-1, 0, -1, 2, 3, -1, 5, 6, 7, -1, 9, 10}, 
        {0, 1, 3, 4, 5, -1, 7, 8, 9, -1, 10, 11}, 
        {-1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    },
    {
        {-1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 7, 8}, 
        {-1, 0, 1, -1, 3, 4, 5, -1, 7, 8, -1, 10}, 
        {0, 1, 2, -1, 4, 5, 6, -1, 7, 8, -1, 9}, 
        {-1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8}
    },
    {
        {-1, -1, -1, -1, 0, 1, 2, 4, 5, 7, -1, -1}, 
        {-1, 0, 1, 2, -1, 4, 5, -1, 7, -1, -1, -1}, 
        {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
        {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
    }
};
    
int pl[4] = {9, 7, 4, 0};

int npl[4][4] = {
    {-1, -1, 5, 8},
    {11, 8, 3, 6},
    {8, 5, 0, 3},
    {4, 1, -1, -1}
};

int n, m, K;
string fi[55];


int dp[53][53][5][4100];

int main() {
    //freopen( "/Users/macuser/Dropbox/Contest/input.in", "r", stdin );
    while (cin >> n >> m >> K) {
        for (int i = 0; i < n; ++i) cin >> fi[i];
        
        int res = 0;
        
        memset(dp, -1, sizeof(dp));
        dp[0][0][0][(1<<9)] = 0;
        for (int k = 0; k <= K; ++k) {
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                    for (int bit = 0; bit < (1<<12); ++bit) {
                        if (dp[i][j][k][bit] == -1) continue;
                        if (i == n-1 && j == m-1) chmax(res, dp[i][j][k][bit]);
                        
                        //cout << i << ", " << j << ", " << k << " ; " << bitset<12>(bit) << " : " << dp[i][j][k][bit] << endl;
                        
                        for (int l = 0; l <= 3; ++l) {
                            int ni = i + dx[l], nj = j + dy[l];
                            if (ni < 0 || ni >= n || nj < 0 || nj >= m) continue;
                            if (fi[ni][nj] == '#') continue;
                            
                            int add = 0;
                            if (fi[ni][nj] != '.') add = fi[ni][nj] - '0';
                            if (npl[k][l] != -1)
                                if (bit & (1<<npl[k][l]))
                                    add = 0;
                            
                            int nk = k;
                            if (l == 2 || l == 3) ++nk;
                            if (nk > K) continue;
                            
                            int nbit = 0;
                            for (int b = 0; b < 12; ++b)
                                if (bit & (1<<b))
                                    if (ch[k][l][b] != -1)
                                        nbit |= (1<<ch[k][l][b]);
                            
                            nbit |= (1<<pl[nk]);
                            
                            chmax(dp[ni][nj][nk][nbit], dp[i][j][k][bit] + add);
                        }
                    }
                }
            }
        }

        cout << res << endl;
    }
    return 0;
}