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;

typedef pair<int, int> Pair;
typedef pair<Pair, Pair> PP;

int H;
int W;
int K;
int ans;
char field[114][514];
vector<Pair> around;
queue<PP> q;
map<PP, bool> calced;
map<Pair, int> rev;
map<int, int> dp[114][114][4];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
bool nocost[] = {0, 0, 1, 1};

bool WithinRange(int y, int x) {
  if (y < 0 || H <= y) return false;
  if (x < 0 || W <= x) return false;
  return true;
}

int ShiftHash(int hash, int dy, int dx) {
  int ret = 0;
  for (Pair p : around) {
    int y, x;
    tie(y, x) = p;
    int py = y + dy;
    int px = x + dx;
    if (rev.count(Pair(py, px))) {
      int v = hash >> rev[Pair(py, px)] & 1;
      ret |= v << rev[p];
    }
  }
  return ret;
}

int main() {
  scanf("%d%d%d", &H, &W, &K);
  for (int i=0; i<H; i++) {
    for (int j=0; j<W; j++) {
      scanf(" %c", &field[i][j]);
      if (field[i][j] == '.') field[i][j] = '0';
    }
  }

  for (int i=-3; i<=3; i++) {
    for (int j=-3; j<=3; j++) {
      if (i == 0 && j == 0) continue;
      if (abs(i)+abs(j) <= 3) {
        rev[Pair(i, j)] = around.size();
        around.emplace_back(Pair(i, j));
      }
    }
  }

  dp[0][0][K][0] = 0;
  q.push(PP(Pair(0, 0), Pair(K, 0)));
  while (!q.empty()) {
    PP pp = q.front(); q.pop();
    int y, x, k, hash;
    tie(y, x) = pp.first;
    tie(k, hash) = pp.second;
    if (field[y][x] == '#') continue;
    if (calced.count(pp)) continue;
    calced[pp] = true;
    
    int val = dp[y][x][k][hash];
    //printf("(%d, %d, %d, %d): %d\n", y, x, k, hash, val);
    if (y == H-1 && x == W-1) {
      ans = max(ans, val);
    }

    for (int i=0; i<4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (!WithinRange(ny, nx)) continue;
      if (field[ny][nx] == '#') continue;

      int plus = field[ny][nx] - '0';
      if (hash >> rev[Pair(dy[i], dx[i])] & 1) plus = 0;
    
      int nk = k;
      if (!nocost[i]) {
        if (k == 0) continue;
        nk = k-1;
      }

      int nhash = ShiftHash(hash, dy[i], dx[i]);
      nhash |= 1 << rev[Pair(-dy[i], -dx[i])];
      for (Pair p : around) {
        int a, b;
        tie(a, b) = p;
        int d = -(min(a, 0)+min(b, 0));
        if (d > nk) nhash &= ~(1 << rev[p]);
      }

      dp[ny][nx][nk][nhash] = max(dp[ny][nx][nk][nhash], val+plus);
      q.push(PP(Pair(ny, nx), Pair(nk, nhash)));
    }     
  }
  printf("%d\n", ans);
}