#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef double db;
ll vx[4]={1,0,-1,0};
ll vy[4]={0,1,0,-1};
struct T{
	ll y,x,k,s;
};
queue<T> q;
ll dp[50][50][4][1<<11];
string str[50];
ll s[50][50];
ll W,H,K;
ll z;
ll S;
ll add;
ll k;
int main()
{
	scanf("%lld%lld%lld",&H,&W,&K);
	memset(dp,-1,sizeof(dp));
	for(ll i=0;i<H;i++){
		cin>>str[i];
		for(ll j=0;j<W;j++){
			if(str[i][j]=='#') s[i][j]=-1;
			else if(str[i][j]=='.') s[i][j]=0;
			else s[i][j]=str[i][j]-'0';
		}
	}
	dp[0][0][3-K][0]=0; //?????¨??????->1
	q.push((T){0,0,3-K,0});
	while(!q.empty()){
		T t=q.front();
		q.pop();
		ll val=dp[t.y][t.x][t.k][t.s];
		k=t.k;
		if(t.k==0){
			if(t.x<W-1&&s[t.y][t.x+1]!=-1){
				add=s[t.y][t.x+1];
				S=t.s;
				S>>=1;
				z=1|(1<<2)|(1<<5);
				S&=~z;
				S|=(1<<8);
				if(dp[t.y][t.x+1][k][S]<val+add){
					dp[t.y][t.x+1][k][S]=val+add;
					q.push((T){t.y,t.x+1,k,S});
				}
			}
			if(t.y<H-1&&s[t.y+1][t.x]!=-1){
				add=s[t.y+1][t.x];
				S=t.s;
				S>>=4;
				z=(1<<3)|(1<<4);
				S&=z;
				S|=((t.s>>2)&1)	| ((t.s&(3<<4))>>3);
				S|=(1<<5);
				if(dp[t.y+1][t.x][k][S]<val+add){
					dp[t.y+1][t.x][k][S]=val+add;
					q.push((T){t.y+1,t.x,k,S});
				}
			}
			if(t.y>0&&s[t.y-1][t.x]!=-1){
				if((t.s>>5)&1) add=0;
				else add=s[t.y-1][t.x];
				S=0;
				z=(1<<5)|(1<<6);
				S|=((t.s<<2)&z);
				S|=(t.s&1);
				z=(1<<2)|(1<<3);
				S|=((t.s<<1)&z);
				z=(1<<8)|(1<<9);
				S|=((t.s<<1)&z);
				S|=(1<<10);
				if(dp[t.y-1][t.x][k+1][S]<val+add){
					dp[t.y-1][t.x][k+1][S]=val+add;
					q.push((T){t.y-1,t.x,k+1,S});
				}
			}
			if(t.x>0&&s[t.y][t.x-1]!=-1){
				S=t.s>>1;
				if((t.s>>8)&1) add=0;
				else add=s[t.y][t.x-1];
				z=(1<<8)|(1<<9)|(1<<10);
				S&=~z;
				S|=(1<<7);
				if(dp[t.y][t.x-1][k+1][S]<val+add){
					dp[t.y][t.x-1][k+1][S]=val+add;
					q.push((T){t.y,t.x-1,k+1,S});
				}
			}
		}
		else if(t.k==1){
			if(t.x<W-1&&s[t.y][t.x+1]!=-1){
				if((t.s>>7)&1) add=0;
				else add=s[t.y][t.x+1];
				S=t.s>>1;
				z=(1<<1)|(1<<4)|(1<<7)|(1<<10);
				S&=~z;
				S|=(1<<6);
				if(dp[t.y][t.x+1][k][S]<val+add){
					dp[t.y][t.x+1][k][S]=val+add;
					q.push((T){t.y,t.x+1,k,S});
				}
			}
			if(t.y<H-1&&s[t.y+1][t.x]!=-1){
				if((t.s>>10)&1) add=0;
				else add=s[t.y+1][t.x];
				S=0;
				z=3;
				S|=(t.s>>3)&z;
				z=(1<<2);
				S|=(t.s>>4)&z;
				z=(1<<4)|(1<<5)|(1<<6);
				S|=(t.s>>3)&z;
				S|=(1<<3);
				if(dp[t.y+1][t.x][k][S]<val+add){
					dp[t.y+1][t.x][k][S]=val+add;
					q.push((T){t.y+1,t.x,k,S});
				}
			}
			if(t.y>0&&s[t.y-1][t.x]!=-1){
				if((t.s>>3)&1) add=0;
				else add=s[t.y-1][t.x];
				S=t.s;
				z=1|(1<<1)|(1<<4)|(1<<6)|(1<<9)|(1<<10);
				S&=z;
				z=(1<<3);
				S|=(t.s<<1)&z;
				S|=(1<<7);
				z|=(1<<8);
				S|=(t.s<<1)&z;
				if(dp[t.y-1][t.x][k+1][S]<val+add){
					dp[t.y-1][t.x][k+1][S]=val+add;
					q.push((T){t.y-1,t.x,k+1,S});
				}
			}
			if(t.x>0&&s[t.y][t.x-1]!=-1){
				if((t.s>>6)&1) add=0;
				else add=s[t.y][t.x-1];
				S=(t.s>>2);
				S|=(1<<4);
				if(dp[t.y][t.x-1][k+1][S]<val+add){
					dp[t.y][t.x-1][k+1][S]=val+add;
					q.push((T){t.y,t.x-1,k+1,S});
				}
			}
		}
		else if(t.k==2){
			if(t.x<W-1&&s[t.y][t.x+1]!=-1){
				if((t.s>>4)&1) add=0;
				else add=s[t.y][t.x+1];
				S=t.s>>1;
				z=(1<<2)|(1<<5)|(1<<8)|(1<<10);
				S&=~z;
				S|=(1<<3);
				if(dp[t.y][t.x+1][k][S]<val+add){
					dp[t.y][t.x+1][k][S]=val+add;
					q.push((T){t.y,t.x+1,k,S});
				}
			}
			if(t.y<H-1&&s[t.y+1][t.x]!=-1){
				if((t.s>>7)&1) add=0;
				else add=s[t.y+1][t.x];
				S=0;
				z=(1<<1)|(1<<2)|(1<<3)|(1<<6)|(1<<7);
				S|=(t.s>>3)&z;
				z=(1<<4);
				S|=(t.s>>4)&z;
				S|=1;
				if(dp[t.y+1][t.x][k][S]<val+add){
					dp[t.y+1][t.x][k][S]=val+add;
					q.push((T){t.y+1,t.x,k,S});
				}
			}
			if(t.y>0&&s[t.y-1][t.x]!=-1){
				if(t.s&1) add=0;
				else add=s[t.y-1][t.x];
				S=0;
				z=1|(1<<1)|(1<<6)|(1<<7);
				S|=(t.s>>1)&z;
				z=(1<<4)|(1<<5);
				S|=(t.s&z);
				z=(1<<8);
				S|=((t.s>>2)&z);
				S|=(1<<3);
				if(dp[t.y-1][t.x][k+1][S]<val+add){
					dp[t.y-1][t.x][k+1][S]=val+add;
					q.push((T){t.y-1,t.x,k+1,S});
				}
			}
			if(t.x>0&&s[t.y][t.x-1]!=-1){
				if((t.s>>3)&1) add=0;
				else add=s[t.y][t.x-1];
				S=t.s>>3;
				S|=1;
				if(dp[t.y][t.x-1][k+1][S]<val+add){
					dp[t.y][t.x-1][k+1][S]=val+add;
					q.push((T){t.y,t.x-1,k+1,S});
				}
			}
		}
		else{
			if(t.x<W-1&&s[t.y][t.x+1]!=-1){
				if(t.s&1) add=0;
				else add=s[t.y][t.x+1];
				S=t.s>>1;
				z=(1<<2)|(1<<5)|(1<<7)|(1<<8);
				S&=~z;
				if(dp[t.y][t.x+1][k][S]<val+add){
					dp[t.y][t.x+1][k][S]=val+add;
					q.push((T){t.y,t.x+1,k,S});
				}
			}
			if(t.y<H-1&&s[t.y+1][t.x]!=-1){
				if((t.s>>3)&1) add=0;
				else add=s[t.y+1][t.x];
				S=0;
				z=3;
				S|=(t.s>>4)&z;
				z=(3<<3);
				S|=(t.s>>3)&z;
				S|=(t.s>>2)&(1<<6);
				if(dp[t.y+1][t.x][k][S]<val+add){
					dp[t.y+1][t.x][k][S]=val+add;
					q.push((T){t.y+1,t.x,k,S});
				}
			}
		}
	}
	ll ans=0;
	for(ll i=0;i<4;i++){
		for(ll j=0;j<(1<<11);j++){
			ans=max(ans,dp[H-1][W-1][i][j]);
		}
	}
	printf("%lld\n",ans);
}

Measured memory_bytes: 163444