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 <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<(b)?(a=b,1):0)
#define chmin(a,b) (a>(b)?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
const int INF = 1<<29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef pair<int,int> pii;
typedef long long ll;

char a[50][50];
int dp[50][50][4][1<<11];
bool visited[50][50][4][1<<11];
int bitMap[4][1<<11][4];
bool canGet[4][1<<11][4];
vector<pii> needCells[4];

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

int h,w,K;

int solve(int y, int x, int k, int S){
  int &r = dp[y][x][k][S];
  if (visited[y][x][k][S]) return r;
  visited[y][x][k][S] = 1;
  int res = -INF;
  if (y==h-1&&x==w-1) res = 0;
  REP(d,4) {
    int nk = k+(d>=2);
    if (nk>K) continue;
    int ny = y+dy[d];
    int nx = x+dx[d];
    if (!valid(ny,nx,h,w)) continue;
    if (a[ny][nx] == '#') continue;
    int score = canGet[k][S][d]&&isdigit(a[ny][nx]) ? a[ny][nx]-'0' : 0;
    chmax(res, solve(ny,nx,nk,bitMap[k][S][d]) + score);
  }
  return r = res;
}

int main() {
  REP(k,4) {
    for (int y=-3; y<=3; ++y) {
      for (int x=-3; x<=3; ++x) {
        if ((y||x) && -min(y,0)-min(x,0) <= 3-k && max(0,y)+max(0,x) <= k)
          needCells[k].push_back(pii(y,x));
      }
    }
  }
  REP(k,4) {
    int n = needCells[k].size();
    REP(S,1<<n) {
      REP(d,4) {
        int nk = k+(d>=2);
        if (nk == 4) continue;
        int T = 0;
        REP(i,n) {
          if (S>>i&1) {
            pii p = needCells[k][i];
            p.first -= dy[d];
            p.second -= dx[d];
            REP(j,needCells[nk].size()) {
              if (needCells[nk][j] == p) {
                T |= 1 << j;
              }
            }
          }
        }
        REP(j,needCells[nk].size()) {
          if (needCells[nk][j] == pii(-dy[d],-dx[d])) {
            T |= 1 << j;
          }
        }
        bitMap[k][S][d] = T;

        canGet[k][S][d] = 1;
        REP(i,n)
          if (S>>i&1 && needCells[k][i]==pii(dy[d],dx[d]))
            canGet[k][S][d] = 0;
      }
    }
  }
  while(cin>>h>>w>>K) {
    REP(i,h) REP(j,w) cin >> a[i][j];
    cout << solve(0,0,0,0) << endl;
  }
  
}