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 repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 2147483600
#define BACK 6

int dp[(1<<(BACK*2))+10][60][60][4];

int main(){
  int h,w,k;
  cin>>h>>w>>k;

  vector<string> vec(h+BACK);
  rep(i,BACK){
    vec[i].resize(w);
    vec[i][0] = '.';
    repl(j,1,w) vec[i][j] = '#';
  }
  rep(i,h) cin>> vec[i+BACK];

  h += BACK;

  fill(dp[0][0][0], dp[1<<(BACK*2)][0][0], -INF);
  dp[0][BACK][0][0] = 0;

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

  rep(kk,k+1) rep(x,h) rep(y,w) rep(prevs, 1<<(BACK*2)) if(dp[prevs][x][y][kk]>=0){ //dbg(mp(mp(x,y), mp(kk,dp[prevs][x][y][kk])));
    bool visited[4] = {0,0,0,0};
    int px=0, py=0;
    rep(i,BACK){
      int m = (prevs>>(2*i))%4;
      px -= dx[m], py -= dy[m];
      rep(j,4) if(px==dx[j] && py==dy[j]){
        visited[j] = true;
        break;
      }
    }

    rep(i,4){
      int nx = x + dx[i], ny = y + dy[i], nk = kk + (i>=2);
      if(nk>k || nx<0 || ny<0 || nx>=h || ny>=w || vec[nx][ny]=='#') continue;
      int ncost = dp[prevs][x][y][kk];
      if(vec[nx][ny]!='.' && !visited[i]) ncost += vec[nx][ny] - '0';
      int nmask = prevs%(1<<(2*(BACK-1)));
      nmask = nmask*4 + i;
      dp[nmask][nx][ny][nk] = max(dp[nmask][nx][ny][nk], ncost);
    }
  }

  int res = 0;
  rep(i,1<<(BACK*2)) res = max(res, dp[i][h-1][w-1][k]);

  cout << res << endl;

  return 0;
}