//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<queue>
#include<cmath>
const int N=12,UP=517;
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define Rep(i,a,b) for(int i=(a);i>=(b);i--)
typedef long long LL;
typedef double db;
using namespace std;
int n,m,d,vv[N],a[N][N],tx[4]={0,0,1,-1},ty[4]={1,-1,0,0},f[N][N][UP];
int vis[N][N][UP],ans,up,xx[N],yy[N],val[UP];
char s[N];
template<typename T>void read(T &x) {
char ch=getchar(); x=0; T f=1;
while(ch!=‘-‘&&(ch<‘0‘||ch>‘9‘)) ch=getchar();
if(ch==‘-‘) f=-1,ch=getchar();
for(;ch>=‘0‘&&ch<=‘9‘;ch=getchar()) x=x*10+ch-‘0‘; x*=f;
}
int in(int x,int y) { return x>=1&&x<=n&&y>=1&&y<=m&&a[x][y]==0; }
struct node {
int x,y,s;
node(int x,int y,int s):x(x),y(y),s(s){}
};
queue<node>que;
void spfa(int x,int y) {
memset(f,127,sizeof(f));
que.push(node(x,y,0));
f[x][y][0]=0;
while(!que.empty()) {
node tp=que.front();
que.pop();
vis[tp.x][tp.y][tp.s]=0;
For(i,0,3) if(in(tp.x+tx[i],tp.y+ty[i])) {
node z=node(tp.x+tx[i],tp.y+ty[i],tp.s);
if(i>=2) {
For(j,1,d) if(xx[j]==max(z.x,tp.x)&&yy[j]<z.y)
z.s^=(1<<(j-1));
}
if(f[z.x][z.y][z.s]>f[tp.x][tp.y][tp.s]+1) {
if(z.x==2&&z.y==5&&z.s==4) {
int debug=1;
}
f[z.x][z.y][z.s]=f[tp.x][tp.y][tp.s]+1;
if(!vis[z.x][z.y][z.s]) {
vis[z.x][z.y][z.s]=1;
que.push(z);
}
}
}
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
read(n); read(m); read(d);
For(i,1,d) read(vv[i]);
For(i,1,UP)
For(j,1,d) if(i&(1<<(j-1)))
val[i]+=vv[j];
For(i,1,n) {
scanf("%s",s+1);
For(j,1,m) {
if(s[j]==‘#‘) a[i][j]=-1;
else a[i][j]=s[j]-‘0‘;
if(a[i][j]>0) xx[a[i][j]]=i,yy[a[i][j]]=j;
}
}
up=(1<<d)-1;
For(i,1,n) For(j,1,m) if(!a[i][j]) {
spfa(i,j);
For(k,1,up)
ans=max(ans,val[k]-f[i][j][k]);
}
printf("%d\n",ans);
return 0;
}
/*
3 8
3
30 -100 30
00000000
010203#0
00000000
*/