#include<bits/stdc++.h>
using namespace std;
char s[101][100001];
int v[101][100001],n,m,k,i,j,x,y,ey,ex,ans=-999,xxx;
int h[4]={0,0,1,-1};
queue<int> xx;
queue<int> qx;
queue<int> qy;
bool p(int x,int xx){
if(v[ex+x][ey+1]==-1)
return 0;
if(xx-v[x][y]<1)
return 0;
if(ex+x>n||ex+x<1)
return 0;
return 1;
}
int pc(char x){
switch(x){
case '.':return 0;
default:return x-48;
case '@':return 0;
case 'l':return -1;
}
return 0;
}
int main(){
cin>>k>>n>>m;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++){
cin>>s[i][j];
v[i][j]=pc(s[i][j]);
if(s[i][j]=='@'){
qy.push(j);
qx.push(i);
xx.push(k);
}
}
while(qy.size()>0){
ey=qy.front(),ex=qx.front(),xxx=xx.front();
qy.pop(),xx.pop(),qx.pop();
if(ey>m){
ans=max(ans,xxx);
continue;
}
for(i=1;i<=3;i++){
if(p(h[i],xxx)){
qx.push(ex+h[i]);
xx.push(xxx-v[ex][ey]);
qy.push(ey+1);
}
}
}
if(ans<1){
cout<<"Wrong"<<endl;
return 0;
}
cout<<ans<<endl;
return 0;
}