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;

enum Block {HOUSE = -1, BLANK};
const int BIT = 12;

int H, W, K;
vector<vector<int>> B;

int dp[50][50][4][1 << BIT];

enum {Y, X};
const int POINT[4][BIT][2] = {
  {{4,4}, {1,4}, {2,3}, {2,4}, {3,2}, {3,3}, {3,4}, {4,1}, {4,2}, {4,3}, {0,0}, {0,0},},
  {{4,4}, {2,4}, {2,5}, {3,3}, {3,4}, {3,5}, {4,2}, {4,3}, {4,5}, {5,2}, {5,3}, {5,4},},
  {{4,4}, {3,4}, {3,5}, {3,6}, {4,3}, {4,5}, {4,6}, {5,3}, {5,4}, {5,5}, {6,3}, {6,4},},
  {{4,4}, {4,5}, {4,6}, {4,7}, {5,4}, {5,5}, {5,6}, {6,4}, {6,5}, {7,4}, {0,0}, {0,0},},
};
enum {STRAIGHT, REVERSE};
const int NEXT[2][2][2] = {
  {{0, 1}, { 1,0},},
  {{0,-1}, {-1,0},},
};

vector<vector<bool>> bit_to_part(int bit, int k) {
  vector<vector<bool>> res(9, vector<bool>(9, false));
  for(int b = 0; b < BIT; ++b) if((bit >> b) & 1) res[POINT[k][b][Y]][POINT[k][b][X]] = true;
  return res;
}

int part_to_bit(const vector<vector<bool>>& part, int k) {
  int res = 0;
  for(int b = 0; b < BIT; ++b) if(part[POINT[k][b][Y]][POINT[k][b][X]]) res |= (1 << b);
  return res;
}

int nextbit(int y, int x, int k, int bit, int dy, int dx, int dk) {
  int ny = y + dy, nx = x + dx;
  int nk = k + dk;
  if(ny < 0 || H <= ny || nx < 0 || W <= nx) return -1;
  if(B[ny][nx] == HOUSE) return -1;
  if(K < nk) return -1;
  auto cur = bit_to_part(bit, k);
  vector<vector<bool>> nex(9, vector<bool>(9, false));
  for(int i = 1; i < 8; ++i) for(int j = 1; j < 8; ++j) nex[i][j] = cur[i + dy][j + dx];
  return part_to_bit(nex, k + dk);
}

int solve() {
  int res = 0;
  for(int i = 0; i < H; ++i) for(int j = 0; j < W; ++j) for(int k = 0; k <= K; ++k) for(int bit = 0; bit < (1 << BIT); ++bit) dp[i][j][k][bit] = -1;
  dp[0][0][0][1] = 0;
  for(int k = 0; k <= K; ++k) {
    for(int y = 0; y < H; ++y) for(int x = 0; x < W; ++x) for(int bit = 0; bit < (1 << BIT); ++bit) {
      if(dp[y][x][k][bit] == -1) continue;
      for(auto d: NEXT[STRAIGHT]) {
        int ny = y + d[Y];
        int nx = x + d[X];
        int nk = k;
        int nbit = nextbit(y, x, k, bit, d[Y], d[X], 0);
        if(nbit < 0) continue;
        int add = 0;
        if(!(nbit & 1) && 0 < B[ny][nx]) add = B[ny][nx];
        nbit |= 1;
          dp[ny][nx][nk][nbit] = max(dp[ny][nx][nk][nbit], add + dp[y][x][k][bit]);
          if(ny == H - 1 && nx == W - 1) res = max(res, dp[ny][nx][nk][nbit]);
      }
    }
    if(k < K) {
      for(int y = H - 1;  0 <= y; --y) for(int x = W - 1; 0 <= x; --x) for(int bit = 0; bit < (1 << BIT); ++bit) {
        if(dp[y][x][k][bit] == -1) continue;
        for(auto d: NEXT[REVERSE]) {
          int ny = y + d[Y];
          int nx = x + d[X];
          int nk = k + 1;
          int nbit = nextbit(y, x, k, bit, d[Y], d[X], 1);
          if(nbit < 0) continue;
          int add = 0;
          if(!(nbit & 1) && 0 < B[ny][nx]) add = B[ny][nx];
          nbit |= 1;
          dp[ny][nx][nk][nbit] = max(dp[ny][nx][nk][nbit], add + dp[y][x][k][bit]);
          if(ny == H - 1 && nx == W - 1) res = max(res, dp[ny][nx][nk][nbit]);
        }
      }
    }
  }
  return res;
}

int main() {
  cin >> H >> W >> K;
  B.assign(H, vector<int>(W));
  for(auto& line: B) for(auto& i: line) {
    char c;
    cin >> c;
    if(c == '.')      i = BLANK;
    else if(c == '#') i = HOUSE;
    else              i = c - '0';
  }
  cout << solve() << endl;
}