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<cstring>
using namespace std;

int h, w, k;
char field[50][50];

int dp[4][50][50][1024][4];
int ans = 0;
int sum[4] = {0, 1, 3, 6};

bool judge(int x, int y)
{
  if(0 <= x && x < h && 0 <= y && y < w && field[x][y] != '#')
    return true;
  return false;
}

void solve(int times)
{
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      for(int l = 0; l < 1024; l++){
	for(int m = 0; m <= times; m++){

	  if(dp[times][i][j][l][m] > -1){
	    //toright
	    if(judge(i, j + 1)){
	      int nextl 
		= (field[i][j + 1] == '.' ? 0 : 1 << (sum[times] + m))
		| ((l & (1 << 0)) << 1)
		| ((l & (1 << 1)) << 2)
		| ((l & (1 << 2)) << 2)
		| ((l & (1 << 3)) << 3)
		| ((l & (1 << 4)) << 3)
		| ((l & (1 << 5)) << 3);
	      
	      dp[times][i][j + 1][nextl][m] = max(dp[times][i][j + 1][nextl][m], dp[times][i][j][l][m] + ((field[i][j + 1] == '.'|| times > 0 && (l & (1 << (sum[times] + m - (times - 1) - 1)))) ? 0 : field[i][j + 1] - '0'));
	    }
	    
	    //tounder
	    if(judge(i + 1, j)){
	      int nextl
		= (field[i + 1][j] == '.' ? 0 : 1 << (sum[times] + m))
		| ((l & (1 << 0)) << 2)
		| ((l & (1 << 1)) << 3)
		| ((l & (1 << 2)) << 3)
		| ((l & (1 << 3)) << 4)
		| ((l & (1 << 4)) << 4)
		| ((l & (1 << 5)) << 4);
	      
	      dp[times][i + 1][j][nextl][m] = max(dp[times][i + 1][j][nextl][m], dp[times][i][j][l][m] + ((field[i + 1][j] == '.' || times > 0 && (l & (1 << (sum[times] + m - (times - 1) - 2)))) ? 0 : field[i + 1][j] - '0'));
	    }
	    
	    if(times < k){
	      //toleft
	      if(judge(i, j - 1)){
		int nextl = l | (field[i][j - 1] == '.' ? 0 : (1 << (sum[times] + m + times + 1)));
		dp[times + 1][i][j - 1][nextl][m] = max(dp[times + 1][i][j - 1][nextl][m], dp[times][i][j][l][m] + ((field[i][j - 1] == '.' || (l & (1 << (sum[times] + m + times + 1)))) ? 0 : field[i][j - 1] - '0'));
	      }
	      
	      //toup
	      if(judge(i - 1, j)){
		int nextl = l | (field[i - 1][j] == '.' ? 0 : (1 << (sum[times] + m + times + 2)));
		dp[times + 1][i - 1][j][nextl][m + 1] = max(dp[times + 1][i - 1][j][nextl][m + 1], dp[times][i][j][l][m] + ((field[i - 1][j] == '.' || (l & (1 << (sum[times] + m + times + 2)))) ? 0 : field[i - 1][j] - '0'));
	      }
	    }

	  }
	}
      }
    }
  }

  for(int i = 0; i < 1024; i++){
    for(int j = 0; j <= times; j++){
      ans = max(ans, dp[times][h - 1][w - 1][i][j]);
    }
  }
}

int main()
{
  cin >> h >> w >> k;
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      cin >> field[i][j];
    }
  }

  memset(dp, -1, sizeof dp);
  dp[0][0][0][0][0] = 0;

  for(int i = 0; i <= k; i++){
    solve(i);
  }

  cout << ans << endl;

  return 0;
}