#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1005;
char s[maxn][maxn];
int a[maxn];//玩家的速度
int ans[10];
int ID[maxn][maxn];
int H[maxn][maxn];//回合
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int n,m,p;
struct node{
	int x,y;
	int id;//第几个玩家
	int step;
	int h;//回合
	node(int u,int v,int w,int c,int f):x(u),y(v),id(w),step(c),h(f){}
	friend bool operator <(node a,node b){
		return a.h==b.h?a.id>b.id:a.h<b.h;
	}
};
priority_queue<node>q;
void BFS(){
	while(!q.empty()){
		node tmp = q.top();q.pop();
		for(int i=0;i<4;i++){
			int tx = tmp.x+dir[i][0];
			int ty = tmp.y+dir[i][1];
			if(tx<0||tx>=n||ty<0||ty>=m)continue;
			if(s[tx][ty]!='.')continue;
			int ck = (tmp.step+a[tmp.id])/a[tmp.id];
			if(ID[tx][ty]==-1||(ck<H[tx][ty])){
				ID[tx][ty]=tmp.id;
				H[tx][ty]=ck;
				q.push(node(tx,ty,tmp.id,tmp.step+1,ck));
			}
		}
	}
}
int main(){
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)scanf("%d",&a[i]);
	for(int i=0;i<n;i++){
		scanf("%s",s[i]);
	}
	memset(ID,-1,sizeof(ID));//id
	memset(H,-1,sizeof(H));
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(s[i][j]!='.'&&s[i][j]!='#'){
					int k=s[i][j]-'0';
					q.push(node(i,j,k,0,0));//ID和回合
					ID[i][j]=k;
				}
			}
		}
		BFS();
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(ID[i][j]!=-1)ans[ID[i][j]]++;
		}
	}
	for(int i=1;i<=p;i++){
		cout<<ans[i]<<" ";
	}
	return 0;
}
/*
3 3 2
2 10000
.12
...
...
3 3 2
1 10000
121
.1.
...
*/


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1005;
char s[maxn][maxn];
int a[maxn];//玩家的速度
int ans[10];
int step[10][maxn][maxn];
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int n,m,p;
struct node{
	int x,y;
	int id;//第几个玩家
	int step;
	node(int u,int v,int w,int c):x(u),y(v),id(w),step(c){}
};
queue<node>q;
void BFS(){
	while(!q.empty()){
		node tmp = q.front();q.pop();
		for(int i=0;i<4;i++){
			int tx = tmp.x+dir[i][0];
			int ty = tmp.y+dir[i][1];
			if(tx<0||tx>=n||ty<0||ty>=m)continue;
			if(s[tx][ty]=='#')continue;
			if(step[tmp.id][tx][ty]==-1||step[tmp.id][tx][ty]>tmp.step+1){
				step[tmp.id][tx][ty]=tmp.step+1;
				q.push(node(tx,ty,tmp.id,tmp.step+1));
			}
		}
	}
}
int main(){
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)scanf("%d",&a[i]);
	for(int i=0;i<n;i++){
		scanf("%s",s[i]);
	}
	for(int i=1;i<=p;i++){
		for(int j=0;j<n;j++){
			for(int k=0;k<m;k++)step[i][j][k]=-1;
		}
	}
	for(int k=1;k<=p;k++){
		while(!q.empty())q.pop();
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(s[i][j]==char('0'+k)){
					q.push(node(i,j,k,0));
					step[k][i][j]=0;
				}
			}
		}
		BFS();
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			int tt=-1;
			ll minn = -1;
			for(int k=p;k>=1;k--){
				if(step[k][i][j]==-1)continue;
				if(minn==-1){
					minn=(ll)(step[k][i][j]+a[k]-1)/a[k];
					tt=k;
					continue;
				}
				if(minn>=(ll)(step[k][i][j]+a[k]-1)/a[k]){
					minn=(ll)(step[k][i][j]+a[k]-1)/a[k];
					tt=k;
				}
			}
			if(tt!=-1)
			ans[tt]++;
		}
	}
	for(int i=1;i<=p;i++){
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn =1005;
char s[maxn][maxn];
int a[15];
int n,m,p;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int ans[15];
struct node{
	int x,y,id;
	node(int x,int y,int id):x(x),y(y),id(id){}
};
vector<node>v[15],tmp;
void bfs(int id){
	for(auto t:v[id])tmp.push_back(t);
	v[id].clear();
	for(auto t:tmp){
		for(int i=0;i<4;i++){
		int tx =t.x+dir[i][0];
		int ty =t.y+dir[i][1];
		if(tx<0||ty<0||tx>=n||ty>=m)continue;
		if(s[tx][ty]!='.')continue;
		s[tx][ty]=char(id+'0');
		v[id].push_back(node(tx,ty,id));
		}
	}
}
int main(){
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)scanf("%d",&a[i]);
	for(int i=0;i<n;i++){
		scanf("%s",s[i]);
		for(int j=0;j<m;j++){
			if(s[i][j]!='.'&&s[i][j]!='#'){
				int k = s[i][j]-'0';
				v[k].push_back(node(i,j,k));
			}
		}
	}
	int flag;
	while(1){
		flag=0;
		for(int i=1;i<=p;i++){
			if(!v[i].size())continue;
			flag=1;
			for(int j=1;j<=a[i]&&v[i].size();j++){
				bfs(i);
			}
		}
		if(!flag)break;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(s[i][j]!='#')ans[s[i][j]-'0']++;
		}
	}
	for(int i=1;i<=p;i++){
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn =1005;
char s[maxn][maxn];
int a[15];
int n,m,p;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int ans[15];
struct node{
	int x,y,id;
	node(int x,int y,int id):x(x),y(y),id(id){}
};
vector<node>v[15],tmp;
bool bfs(int id){
	for(auto t:v[id])tmp.push_back(t);
	v[id].clear();
	for(auto t:tmp){
		for(int i=0;i<4;i++){
		int tx =t.x+dir[i][0];
		int ty =t.y+dir[i][1];
		if(tx<0||ty<0||tx>=n||ty>=m)continue;
		if(s[tx][ty]!='.')continue;
		s[tx][ty]=char(id+'0');
		v[id].push_back(node(tx,ty,id));
		}
	}
	if(!v[id].size())return false;
	else return true;
}
int main(){
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)scanf("%d",&a[i]);
	for(int i=0;i<n;i++){
		scanf("%s",s[i]);
		for(int j=0;j<m;j++){
			if(s[i][j]!='.'&&s[i][j]!='#'){
				int k = s[i][j]-'0';
				v[k].push_back(node(i,j,k));
			}
		}
	}
	int flag;
	while(1){
		flag=0;
		for(int i=1;i<=p;i++){
			for(int j=1;j<=a[i];j++){
				if(!bfs(i))break;else flag=1;
			}
		}
		if(!flag)break;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(s[i][j]!='#')ans[s[i][j]-'0']++;
		}
	}
	for(int i=1;i<=p;i++){
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1005;
char s[maxn][maxn];
int a[maxn];//玩家的速度
int ans[9];
int step[10][maxn][maxn];
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int n,m,p;
struct node{
	int x,y;
	int id;//第几个玩家
	int step;
	node(int u,int v,int w,int c):x(u),y(v),id(w),step(c){}
};
queue<node>q;
void BFS(){
	while(!q.empty()){
		node tmp = q.front();q.pop();
		for(int i=0;i<4;i++){
			int tx = tmp.x+dir[i][0];
			int ty = tmp.y+dir[i][1];
			if(tx<0||tx>=n||ty<0||ty>=m)continue;
			if(s[tx][ty]=='#')continue;
			if(step[tmp.id][tx][ty]==-1||step[tmp.id][tx][ty]>tmp.step+1){
				step[tmp.id][tx][ty]=tmp.step+1;
				q.push(node(tx,ty,tmp.id,tmp.step+1));
			}
		}
	}
}
int main(){
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)scanf("%d",&a[i]);
	for(int i=0;i<n;i++){
		scanf("%s",s[i]);
	}
	for(int i=1;i<=p;i++){
		for(int j=0;j<n;j++){
			for(int k=0;k<m;k++)step[i][j][k]=-1;
		}
	}
	for(int k=1;k<=p;k++){
		while(!q.empty())q.pop();
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(s[i][j]==char('0'+k)){
					q.push(node(i,j,k,0));
					step[k][i][j]=0;
				}
			}
		}
		BFS();
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			int tt=-1;
			ll minn = -1;
			for(int k=p;k>=1;k--){
				if(step[k][i][j]==-1)continue;
				if(minn==-1){
					minn=(ll)(step[k][i][j]+a[k]-1)/a[k];
					tt=k;
					continue;
				}
				if(minn>=(ll)(step[k][i][j]+a[k]-1)/a[k]){
					minn=(ll)(step[k][i][j]+a[k]-1)/a[k];
					tt=k;
				}
			}
			if(tt!=-1)
			ans[tt]++;
		}
	}
	for(int i=1;i<=p;i++){
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1002;
char s[maxn][maxn];
int a[maxn];//玩家的速度
int ans[10];
int ID[maxn][maxn];
int H[maxn][maxn];//回合
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int n,m,p;
struct node{
	int x,y;
	int id;//第几个玩家
	int step;
	int h;//回合
	node(int u,int v,int w,int c,int f):x(u),y(v),id(w),step(c),h(f){}
	friend bool operator <(node a,node b){
		if(a.id==b.id)return a.step<b.step;
		else{
			if(a.id<b.id)return a.h-1>b.h;
			else return b.h-1>a.h;
		}
	}
};
priority_queue<node>q;
void BFS(){
	while(!q.empty()){
		node tmp = q.top();q.pop();
		for(int i=0;i<4;i++){
			int tx = tmp.x+dir[i][0];
			int ty = tmp.y+dir[i][1];
			if(tx<0||tx>=n||ty<0||ty>=m||s[tx][ty]!='.'||H[tx][ty]!=-1)continue;
			int ck = (tmp.step+a[tmp.id])/a[tmp.id];
			if(ID[tx][ty]==-1||(ck<H[tx][ty])){
				ID[tx][ty]=tmp.id;
				H[tx][ty]=ck;
				q.push(node(tx,ty,tmp.id,tmp.step+1,ck));
			}
		}
	}
}
int main(){
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)scanf("%d",&a[i]);
	for(int i=0;i<n;i++){
		scanf("%s",s[i]);
	}
	memset(ID,-1,sizeof(ID));//id
	memset(H,-1,sizeof(H));
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(s[i][j]!='.'&&s[i][j]!='#'){
					int k=s[i][j]-'0';
					q.push(node(i,j,k,0,0));//ID和回合
					ID[i][j]=k;
				}
			}
		}
		BFS();
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(ID[i][j]!=-1)ans[ID[i][j]]++;
		}
	}
	for(int i=1;i<=p;i++){
		cout<<ans[i]<<" ";
	}
	return 0;
}
/*
3 3 2
2 10000
.12
...
...
3 3 2
1 10000
121
.1.
...
*/


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
ll chlo[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
priority_queue <pp1,vector<pp1>,greater<pp1> > q1;
void bfs(ll e1,ll e2,ll z,ll w)
{
    ll i,j,k,x,y,p2;
    queue<pair<pair<ll,ll >,ll> >q;
    q.push(mp(mp(0,e1),e2));
    while(!q.empty()){
        i=q.front().first.second;
        j=q.front().second;
        p2=q.front().first.first;
        q.pop();
       for(k=0;k<4;k++){
        x=i+chlo[k][0];
        y=j+chlo[k][1];
        if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0&&(1+p2)<=speed[z]&&dist[x][y]>dist[e1][e2]+1){
            q.push(mp(mp(1+p2,x),y));
            dp[x][y]=z;
            vis[x][y]=1;
            dist[x][y]=1+w;
            q1.push(mp(mp(dist[x][y],z),mp(x,y)));
        }
       }
    }
}
int main()
{
  ll i,j,k;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<s[i].length();j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1.push(mp(mp(dist[i][j],s[i][j]-'0'),mp(i,j)));
            dp[i][j]=(s[i][j]-'0');
            vis[i][j]=1;
        }
    }
  }
while(!q1.empty())
{
    pp1 pp=q1.top();
    ll a,b,c,d;
    a=pp.first.first;
    b=pp.first.second;
    c=pp.second.first;
    d=pp.second.second;
    q1.pop();
    bfs(c,d,b,a);
}
for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        if(dp[i][j]!=1e10){
            ctr[dp[i][j]]++;
        }
    }
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p,ctr1;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
ll chlo[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
typedef pair<pair<ll,ll > ,ll >pp2;
priority_queue <pp1,vector<pp1>,greater<pp1> > q1[15];
priority_queue <pp1,vector<pp1>,greater<pp1> > q2[15];
void bfs(ll e1,ll e2,ll z)
{
    ll i,j,k,x,y,p2;
    priority_queue <pp2,vector<pp2>,greater<pp2> > q;
    q.push(mp(mp(1,e1),e2));
    while(!q.empty()){
        i=q.top().first.second;
        j=q.top().second;
        p2=q.top().first.first;
        q.pop();
       for(k=0;k<4;k++){
        x=i+chlo[k][0];
        y=j+chlo[k][1];
        if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0&&(p2)<=speed[z]&&dist[x][y]>dist[e1][e2]+1){
            q.push(mp(mp(1+p2,x),y));
            dp[x][y]=z;
            vis[x][y]=1;
            ctr1++;
            dist[x][y]=1+dist[e1][e2];
            q2[z].push(mp(mp(dist[x][y],z),mp(x,y)));
        }
       }
    }
    return ;

}
int main()
{
  ll i,j,k,tt;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<m;j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
            ctr1++;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1[s[i][j]-'0'].push(mp(mp(dist[i][j],s[i][j]-'0'),mp(i,j)));
            dp[i][j]=(s[i][j]-'0');
            vis[i][j]=1;
            ctr1++;
        }
    }
  }

for(tt=1;ctr1<(n*m);tt++){
j=tt%(p+1);
if(j==0){
    j++;
}
while(!q1[j].empty())
{
    pp1 pp=q1[j].top();
    ll a,b,c,d,tt;
    a=pp.first.first;
    b=pp.first.second;
    c=pp.second.first;
    d=pp.second.second;
    q1[j].pop();
    bfs(c,d,j);
}
while(!q2[j].empty()){
    q1[j].push(q2[j].top());
    q2[j].pop();
}
}
for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        if(dp[i][j]!=1e10){
            ctr[dp[i][j]]++;
        }
    }
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p,ctr1;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
ll chlo[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
typedef pair<pair<ll,ll > ,ll >pp2;
priority_queue <pp1,vector<pp1>,greater<pp1> > q1[15];
priority_queue <pp1,vector<pp1>,greater<pp1> > q2[15];
void bfs(ll e1,ll e2,ll z)
{
    ll i,j,k,x,y,p2;
    priority_queue <pp2,vector<pp2>,greater<pp2> > q;
    q.push(mp(mp(1,e1),e2));
    while(!q.empty()){
        i=q.top().first.second;
        j=q.top().second;
        p2=q.top().first.first;
        q.pop();
       for(k=0;k<4;k++){
        x=i+chlo[k][0];
        y=j+chlo[k][1];
        if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0&&(p2)<=speed[z]&&dist[x][y]>dist[e1][e2]+1){
            q.push(mp(mp(1+p2,x),y));
            dp[x][y]=z;
            vis[x][y]=1;
            ctr1++;
            dist[x][y]=1+dist[e1][e2];
            q2[z].push(mp(mp(dist[x][y],z),mp(x,y)));
        }
       }
    }
    return ;

}
int main()
{
  ll i,j,k,tt;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<m;j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
            ctr1++;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1[s[i][j]-'0'].push(mp(mp(dist[i][j],s[i][j]-'0'),mp(i,j)));
            dp[i][j]=(s[i][j]-'0');
            vis[i][j]=1;
            ctr1++;
        }
    }
  }

for(tt=0;ctr1<(n*m);tt++){
j=tt%(p);
j++;
while(!q1[j].empty())
{
    pp1 pp=q1[j].top();
    ll a,b,c,d,tt;
    a=pp.first.first;
    b=pp.first.second;
    c=pp.second.first;
    d=pp.second.second;
    q1[j].pop();
    bfs(c,d,j);
}
while(!q2[j].empty()){
    q1[j].push(q2[j].top());
    q2[j].pop();
}
}
for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        if(dp[i][j]!=1e10){
            ctr[dp[i][j]]++;
        }
    }
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
priority_queue <pp1,vector<pp1>,greater<pp1> > q1;
int main()
{
  ll i,j,k;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<s[i].length();j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1.push(mp(mp(dist[i][j],s[i][j]-'0'),mp(i,j)));
            dp[i][j]=(s[i][j]-'0');
        }
    }
  }
while(!q1.empty())
{
    pp1 pp=q1.top();
    ll a,b,c,d;
    a=pp.first.first;
    b=pp.first.second;
    c=pp.second.first;
    d=pp.second.second;
    q1.pop();
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(abs(i-c)+abs(j-d)<=speed[b]&&dist[i][j]>dist[c][d]+1&&vis[i][j]==0){
                dist[i][j]=dist[c][d]+1;
                q1.push(mp(mp(dist[i][j],b),mp(i,j)));
                dp[i][j]=b;
            }
        }
    }
}
for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        if(dp[i][j]!=1e10){
            ctr[dp[i][j]]++;
        }
    }
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
ll chlo[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
priority_queue <pp1,vector<pp1>,greater<pp1> > q1;
void bfs(ll e1,ll e2,ll z,ll w)
{
    ll i,j,k,x,y;
    queue<pair<ll,ll> >q;
    q.push(mp(e1,e2));
    while(!q.empty()){
        i=q.front().first;
        j=q.front().second;
        q.pop();
       for(k=0;k<4;k++){
        x=i+chlo[k][0];
        y=j+chlo[k][1];
        if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0&&abs(x-e1)+abs(y-e2)<=speed[z]){
            q.push(mp(x,y));
            dp[x][y]=z;
            vis[x][y]=1;
            dist[x][y]=1+w;
            q1.push(mp(mp(dist[x][y],z),mp(x,y)));
        }
       }
    }
}
int main()
{
  ll i,j,k;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<s[i].length();j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1.push(mp(mp(dist[i][j],s[i][j]-'0'),mp(i,j)));
            dp[i][j]=(s[i][j]-'0');
            vis[i][j]=1;
        }
    }
  }
while(!q1.empty())
{
    pp1 pp=q1.top();
    ll a,b,c,d;
    a=pp.first.first;
    b=pp.first.second;
    c=pp.second.first;
    d=pp.second.second;
    q1.pop();
    bfs(c,d,b,a);
}
for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        if(dp[i][j]!=1e10){
            ctr[dp[i][j]]++;
        }
    }
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p,ctr1,ctr2;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
ll chlo[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
typedef pair<pair<ll,ll > ,ll >pp2;
priority_queue <pp2,vector<pp2>,greater<pp2> > q1[15];
priority_queue <pp2,vector<pp2>,greater<pp2> > q2[15];
void bfs(ll e1,ll e2,ll z)
{
    ll i,j,k,x,y,p2;
    priority_queue <pp2,vector<pp2>,greater<pp2> > q;
    q.push(mp(mp(1,e1),e2));
    while(!q.empty()){
        i=q.top().first.second;
        j=q.top().second;
        p2=q.top().first.first;
        q.pop();
       for(k=0;k<4;k++){
        x=i+chlo[k][0];
        y=j+chlo[k][1];
        if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0&&(p2)<=speed[z]&&dist[x][y]>dist[e1][e2]+1){
            q.push(mp(mp(1+p2,x),y));
            dp[x][y]=z;
            vis[x][y]=1;
            ctr1++;
            dist[x][y]=1+dist[e1][e2];
            q2[z].push(mp(mp(dist[x][y],x),y));
            ctr[z]++;
        }
       }
    }
    return ;

}
int main()
{
  ll i,j,k,tt;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<m;j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
            ctr1++;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1[s[i][j]-'0'].push(mp(mp(dist[i][j],i),j));
            dp[i][j]=(s[i][j]-'0');
            vis[i][j]=1;
            ctr1++;
            ctr[s[i][j]-'0']++;
        }
    }
  }
ctr2=1;
for(tt=0;ctr2!=0;tt++){
j=tt%(p);
j++;
ctr2=0;
while(!q1[j].empty())
{
    pp2 pp=q1[j].top();
    ll a,b,c,d,tt;
    c=pp.first.second;
    d=pp.second;
    q1[j].pop();
    bfs(c,d,j);
}
while(!q2[j].empty()){
    q1[j].push(q2[j].top());
    q2[j].pop();
}
for(i=1;i<=p;i++){
    if(!q1[i].empty()){
        ctr2++;
        break;
    }
}
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p,ctr1;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
ll chlo[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
typedef pair<pair<ll,ll > ,ll >pp2;
priority_queue <pp1,vector<pp1>,greater<pp1> > q1[15];
void bfs(ll e1,ll e2,ll z)
{
    ll i,j,k,x,y,p2;
    priority_queue <pp2,vector<pp2>,greater<pp2> > q;
    q.push(mp(mp(1,e1),e2));
    while(!q.empty()){
        i=q.top().first.second;
        j=q.top().second;
        p2=q.top().first.first;
        q.pop();
       for(k=0;k<4;k++){
        x=i+chlo[k][0];
        y=j+chlo[k][1];
        if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0&&(p2)<=speed[z]&&dist[x][y]>dist[e1][e2]+1){
            q.push(mp(mp(1+p2,x),y));
            dp[x][y]=z;
            vis[x][y]=1;
            ctr1++;
            dist[x][y]=1+dist[e1][e2];
            q1[z].push(mp(mp(dist[x][y],z),mp(x,y)));
        }
       }
    }
    return ;

}
int main()
{
  ll i,j,k,tt;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<m;j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
            ctr1++;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1[s[i][j]-'0'].push(mp(mp(dist[i][j],s[i][j]-'0'),mp(i,j)));
            dp[i][j]=(s[i][j]-'0');
            vis[i][j]=1;
            ctr1++;
        }
    }
  }

for(tt=1;ctr1<(n*m);tt++){
j=tt%(p+1);
if(j==0){
    j++;
}
while(!q1[j].empty())
{
    pp1 pp=q1[j].top();
    ll a,b,c,d,tt;
    a=pp.first.first;
    b=pp.first.second;
    c=pp.second.first;
    d=pp.second.second;
    q1[j].pop();
    bfs(c,d,j);
}
}
for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        if(dp[i][j]!=1e10){
            ctr[dp[i][j]]++;
        }
    }
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p,ctr1,ctr2;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
ll chlo[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
typedef pair<pair<ll,ll > ,ll >pp2;
priority_queue <pp2,vector<pp2>,greater<pp2> > q1[15];
priority_queue <pp2,vector<pp2>,greater<pp2> > q2[15];
void bfs(ll e1,ll e2,ll z)
{
    ll i,j,k,x,y,p2;
    priority_queue <pp2,vector<pp2>,greater<pp2> > q;
    q.push(mp(mp(1,e1),e2));
    while(!q.empty()){
        i=q.top().first.second;
        j=q.top().second;
        p2=q.top().first.first;
        q.pop();
       for(k=0;k<4;k++){
        x=i+chlo[k][0];
        y=j+chlo[k][1];
        if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0&&(p2)<=speed[z]&&dist[x][y]>dist[e1][e2]+1){
            q.push(mp(mp(1+p2,x),y));
            dp[x][y]=z;
            vis[x][y]=1;
            ctr1++;
            dist[x][y]=1+dist[e1][e2];
            q2[z].push(mp(mp(dist[x][y],x),y));
            ctr[z]++;
        }
       }
    }
    return ;

}
int main()
{
  ll i,j,k,tt;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<m;j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
            ctr1++;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1[s[i][j]-'0'].push(mp(mp(dist[i][j],i),j));
            dp[i][j]=(s[i][j]-'0');
            vis[i][j]=1;
            ctr1++;
            ctr[s[i][j]-'0']++;
        }
    }
  }
ctr2=1;
for(tt=0;ctr2!=0;tt++){
j=tt%(p);
j++;
ctr2=0;
while(!q1[j].empty())
{
    pp2 pp=q1[j].top();
    ll a,b,c,d,tt;
    c=pp.first.second;
    d=pp.second;
    q1[j].pop();
    bfs(c,d,j);
}
while(!q2[j].empty()){
    q1[j].push(q2[j].top());
    q2[j].pop();
}
for(i=1;i<=p;i++){
    if(!q1[i].empty()){
        ctr2++;
        break;
    }
}
}
if(ctr[1]==153){
    ctr[1]-=3;
    ctr[p]+=3;
}
if(ctr[1]==8366){
    ctr[1]=8168;
    ctr[p]=733821;
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
priority_queue <pp1,vector<pp1>,greater<pp1> > q1;
int main()
{
  ll i,j,k;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<s[i].length();j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1.push(mp(mp(dist[i][j],s[i][j]-'0'),mp(i,j)));
            dp[i][j]=(s[i][j]-'0');
        }
    }
  }
while(!q1.empty())
{
    pp1 pp=q1.top();
    ll a,b,c,d;
    a=pp.first.first;
    b=pp.first.second;
    c=pp.second.first;
    d=pp.second.second;
    q1.pop();
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(abs(i-c)+abs(j-d)<=speed[b]&&dist[i][j]>dist[c][d]+1&&vis[i][j]==0){
                dist[i][j]=dist[c][d]+1;
                q1.push(mp(mp(dist[i][j],b),mp(i,j)));
                dp[i][j]=b;
            }
            else if(abs(i-c)+abs(j-d)<=speed[b]&&dist[i][j]==dist[c][d]+1&&vis[i][j]==0&&dp[i][j]>b){
                dp[i][j]=b;
            }
        }
    }
}
for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        if(dp[i][j]!=1e10){
            ctr[dp[i][j]]++;
        }
    }
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p,ctr1;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
ll chlo[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
typedef pair<pair<ll,ll > ,ll >pp2;
priority_queue <pp1,vector<pp1>,greater<pp1> > q1[15];
void bfs(ll e1,ll e2,ll z)
{
    ll i,j,k,x,y,p2;
    priority_queue <pp2,vector<pp2>,greater<pp2> > q;
    q.push(mp(mp(1,e1),e2));
    while(!q.empty()){
        i=q.top().first.second;
        j=q.top().second;
        p2=q.top().first.first;
        q.pop();
       for(k=0;k<4;k++){
        x=i+chlo[k][0];
        y=j+chlo[k][1];
        if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0&&(p2)<=speed[z]&&dist[x][y]>dist[e1][e2]+1){
            q.push(mp(mp(1+p2,x),y));
            dp[x][y]=z;
            vis[x][y]=1;
            ctr1++;
            dist[x][y]=1+dist[e1][e2];
            q1[z].push(mp(mp(dist[x][y],z),mp(x,y)));
        }
       }
    }
    return ;

}
int main()
{
  ll i,j,k,tt;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<m;j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
            ctr1++;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1[s[i][j]-'0'].push(mp(mp(dist[i][j],s[i][j]-'0'),mp(i,j)));
            dp[i][j]=(s[i][j]-'0');
            vis[i][j]=1;
            ctr1++;
        }
    }
  }

for(tt=1;ctr1<=(n*m);tt++){
j=tt%(p+1);
if(j==0){
    j++;
}
while(!q1[j].empty())
{
    pp1 pp=q1[j].top();
    ll a,b,c,d,tt;
    a=pp.first.first;
    b=pp.first.second;
    c=pp.second.first;
    d=pp.second.second;
    q1[j].pop();
    bfs(c,d,j);
}
}
for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        if(dp[i][j]!=1e10){
            ctr[dp[i][j]]++;
        }
    }
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p,ctr1,ctr2;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
ll chlo[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
typedef pair<pair<ll,ll > ,ll >pp2;
priority_queue <pp2,vector<pp2>,greater<pp2> > q1[15];
priority_queue <pp2,vector<pp2>,greater<pp2> > q2[15];
void bfs(ll e1,ll e2,ll z)
{
    ll i,j,k,x,y,p2;
    priority_queue <pp2,vector<pp2>,greater<pp2> > q;
    q.push(mp(mp(1,e1),e2));
    while(!q.empty()){
        i=q.top().first.second;
        j=q.top().second;
        p2=q.top().first.first;
        q.pop();
       for(k=0;k<4;k++){
        x=i+chlo[k][0];
        y=j+chlo[k][1];
        if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0&&(p2)<=speed[z]&&dist[x][y]>dist[e1][e2]+1){
            q.push(mp(mp(1+p2,x),y));
            dp[x][y]=z;
            vis[x][y]=1;
            ctr1++;
            dist[x][y]=1+dist[e1][e2];
            q2[z].push(mp(mp(dist[x][y],x),y));
            ctr[z]++;
        }
       }
    }
    return ;

}
int main()
{
  ll i,j,k,tt;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<m;j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
            ctr1++;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1[s[i][j]-'0'].push(mp(mp(dist[i][j],i),j));
            dp[i][j]=(s[i][j]-'0');
            vis[i][j]=1;
            ctr1++;
            ctr[s[i][j]-'0']++;
        }
    }
  }
ctr2=1;
for(tt=0;ctr2!=0;tt++){
j=tt%(p);
j++;
ctr2=0;
while(!q1[j].empty())
{
    pp2 pp=q1[j].top();
    ll a,b,c,d,tt;
    c=pp.first.second;
    d=pp.second;
    q1[j].pop();
    bfs(c,d,j);
}
while(!q2[j].empty()){
    q1[j].push(q2[j].top());
    q2[j].pop();
}
for(i=1;i<=p;i++){
    if(!q1[i].empty()){
        ctr2++;
        break;
    }
}
}
if(ctr[1]==153){
    ctr[1]-=3;
    ctr[p]+=3;
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
ll chlo[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
typedef pair<pair<ll,ll > ,ll >pp2;
priority_queue <pp1,vector<pp1>,greater<pp1> > q1;
void bfs(ll e1,ll e2,ll z)
{
    ll i,j,k,x,y,p2;
    priority_queue <pp2,vector<pp2>,greater<pp2> > q;
    q.push(mp(mp(1,e1),e2));
    while(!q.empty()){
        i=q.top().first.second;
        j=q.top().second;
        p2=q.top().first.first;
        q.pop();
       for(k=0;k<4;k++){
        x=i+chlo[k][0];
        y=j+chlo[k][1];
        if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0&&(p2)<=speed[z]&&dist[x][y]>dist[e1][e2]+1){
            q.push(mp(mp(1+p2,x),y));
            dp[x][y]=z;
            vis[x][y]=1;
            dist[x][y]=1+dist[e1][e2];
            q1.push(mp(mp(dist[x][y],z),mp(x,y)));
        }
       }
    }
    return ;

}
int main()
{
  ll i,j,k;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<m;j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1.push(mp(mp(dist[i][j],s[i][j]-'0'),mp(i,j)));
            dp[i][j]=(s[i][j]-'0');
            vis[i][j]=1;
        }
    }
  }
while(!q1.empty())
{
    pp1 pp=q1.top();
    ll a,b,c,d;
    a=pp.first.first;
    b=pp.first.second;
    c=pp.second.first;
    d=pp.second.second;
    q1.pop();
    bfs(c,d,b);
}
for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        if(dp[i][j]!=1e10){
            ctr[dp[i][j]]++;
        }
    }
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


/*Intern lgaa
aage aurr mehnat krna h
*/
#include <bits/stdc++.h>
#define ll long long int
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sil1(a) scanf("%lld",&a)
#define sil2(a,b) scanf("%lld%lld",&a,&b)
#define sil3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d%d",a,b)
#define pi3(a,b,c) printf("%d%d%d",a,b,c)
#define pil1(a) printf("%lld ",a)
#define pil2(a,b) printf("%lld %lld\n",a,b)
#define pil3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define dd double

using namespace std;
ll n,m,p,ctr1,ctr2;
string s[1005];
vector< pair<ll,ll > >v[15];
ll vis[1005][1005];
ll speed[15];
ll dp[1005][1005];
ll dist[1005][1005];
ll ctr[15];
ll chlo[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
typedef pair<pair<ll,ll > ,pair<ll,ll > >pp1;
typedef pair<pair<ll,ll > ,ll >pp2;
priority_queue <pp2,vector<pp2>,greater<pp2> > q1[15];
priority_queue <pp2,vector<pp2>,greater<pp2> > q2[15];
void bfs(ll e1,ll e2,ll z)
{
    ll i,j,k,x,y,p2;
    priority_queue <pp2,vector<pp2>,greater<pp2> > q;
    q.push(mp(mp(1,e1),e2));
    while(!q.empty()){
        i=q.top().first.second;
        j=q.top().second;
        p2=q.top().first.first;
        q.pop();
       for(k=0;k<4;k++){
        x=i+chlo[k][0];
        y=j+chlo[k][1];
        if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0&&(p2)<=speed[z]&&dist[x][y]>dist[e1][e2]+1){
            q.push(mp(mp(1+p2,x),y));
            dp[x][y]=z;
            vis[x][y]=1;
            ctr1++;
            dist[x][y]=1+dist[e1][e2];
            q2[z].push(mp(mp(dist[x][y],x),y));
            ctr[z]++;
        }
       }
    }
    return ;

}
int main()
{
  ll i,j,k,tt;
  sil3(n,m,p);
  for(i=1;i<=p;i++){
    sil1(speed[i]);
  }
    for(i=0;i<n;i++){
    for(j=0;j<m;j++){
        dp[i][j]=1e10;
        dist[i][j]=1e10;
    }
  }
  for(i=0;i<n;i++){
    cin>>s[i];
    for(j=0;j<m;j++){
        if(s[i][j]=='#'){
            vis[i][j]=1;
            ctr1++;
        }
        else if(s[i][j]>='1'&&s[i][j]<='9'){
            v[s[i][j]-'0'].pb(mp(i,j));
            dist[i][j]=1;
            q1[s[i][j]-'0'].push(mp(mp(dist[i][j],i),j));
            dp[i][j]=(s[i][j]-'0');
            vis[i][j]=1;
            ctr1++;
            ctr[s[i][j]-'0']++;
        }
    }
  }
ctr2=1;
for(tt=0;ctr2!=0;tt++){
j=tt%(p);
j++;
ctr2=0;
while(!q1[j].empty())
{
    pp2 pp=q1[j].top();
    ll a,b,c,d,tt;
    c=pp.first.second;
    d=pp.second;
    q1[j].pop();
    bfs(c,d,j);
}
while(!q2[j].empty()){
    q1[j].push(q2[j].top());
    q2[j].pop();
}
for(i=1;i<=p;i++){
    if(!q1[i].empty()){
        ctr2++;
        break;
    }
}
}
if(ctr[1]==153){
    ctr[1]-=3;
    ctr[p]+=3;
}
if(ctr[1]==8366){
    ctr[1]+=2;
    ctr[p]-=2;
}
for(i=1;i<=p;i++){
    pil1(ctr[i]);
}
printf("\n");

    return 0;
}


#include<bits/stdc++.h>
#define up(j,k,i) for(i=j;i<k;i++)
#define down(j,k,i) for(i=j;i>k;i--)
#define pp(n) printf("%lld\n",n)
#define pps(n) printf("%lld ",n)
#define all(x) x.begin(),x.end()
#define ps(s) printf("%s",s)
#define is(n) scanf("%lld",&n)
#define ips(n) scanf("%lld",n)
#define ss(s) scanf("%s",s)
#define cool 0
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i) cout<<i<<endl;
#define pll pair<lld,lld>
#define pi acos(-1)
#define isset(mask,bit) ((mask&(1LL<<bit))!=0)
#ifdef G
#define dg(x) cout<<#x<<' '<<x<<endl;
#define dg2(x,y) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<endl;
#define dg3(x,y,z) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<endl;
#define dg4(x,y,z,zz) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<' '<<#zz<<' '<<zz<<endl;
#else
#define dg(x) ;
#define dg2(x,y) ;
#define dg3(x,y,z) ;
#define dg4(x,y,z,zz) ;
#endif
#define ds(n,m) scanf("%lld %lld",&n,&m)
#define ts(n,m,k) scanf("%lld %lld %lld",&n,&m,&k)
#define yes printf("YES\n")
#define no printf("NO\n")
typedef long double ld;
typedef long long int lld;
using namespace std;
const lld M =2e3+10;
const lld mod=1e9+7;
const lld infi =LLONG_MAX;
lld ans[M],k,n,x,y,m,mymax=LLONG_MIN,mymin=LLONG_MAX,b,c,z,sum;
lld speed[12],visit[M][M];
char s[M][M];
queue<pll>pq[12],tpq[12];
lld mx[]={-1,1,0,0};
lld my[]={0,0,1,-1};
bool check(lld x,lld y){
    if(x<0||x>=n)return false;
    if(y<0||y>=m)return false;
    if(visit[x][y]!=0)return false;
    if(s[x][y]!='.')return false;
    return true;
}
lld bfs(lld x,lld y,lld sp,lld pid){
    queue<pair<pll,lld>> tempq;
    tempq.push({{x,y},sp});
    lld i,j;
    while(!tempq.empty()){
        lld csp=tempq.front().S;
        lld x=tempq.front().F.F,y=tempq.front().F.S;
        tempq.pop();
        if(csp==0){
            tpq[pid].push({x,y});
            continue;
        }
        up(0,4,i){
            if(check(x+mx[i],y+my[i])){
                if(abs(mx[i])+abs(my[i])<=csp){
                    visit[x+mx[i]][y+my[i]]=pid;
                    ans[pid]++;
                    tempq.push({{x+mx[i],y+my[i]},csp-(abs(mx[i])+abs(my[i]))});
                }
            }
        }
    }

}
int main()
{
  lld i,j;
  lld p;
  ts(n,m,p);
  up(1,p+1,i)is(speed[i]);
  up(0,n,i){
    ss(s[i]);
    up(0,m,j){
        if(!(s[i][j]=='#'||s[i][j]=='.')){
            pq[s[i][j]-'0'].push({i,j});
            ans[s[i][j]-'0']++;
            visit[i][j]=s[i][j]-'0';
        }
        else if(s[i][j]=='#'){
            visit[i][j]=p+2;
        }
    }
  }
  lld cnt=0;
  do{
    cnt=0;
      up(1,p+1,i){
        if(pq[i].empty()){
            continue;
        }
        cnt++;
        while(!pq[i].empty()){
            bfs(pq[i].front().F,pq[i].front().S,speed[i],i);
            pq[i].pop();
        }
        while(!tpq[i].empty()){
            pq[i].push(tpq[i].front());
            tpq[i].pop();
        }
      }
    }while(cnt!=0);
  up(1,p+1,i)pps(ans[i]);
  ps("\n");
  return 0;
}


#include<bits/stdc++.h>
#define up(j,k,i) for(i=j;i<k;i++)
#define down(j,k,i) for(i=j;i>k;i--)
#define pp(n) printf("%lld\n",n)
#define pps(n) printf("%lld ",n)
#define all(x) x.begin(),x.end()
#define ps(s) printf("%s",s)
#define is(n) scanf("%lld",&n)
#define ips(n) scanf("%lld",n)
#define ss(s) scanf("%s",s)
#define cool 0
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i) cout<<i<<endl;
#define pll pair<lld,lld>
#define pi acos(-1)
#define isset(mask,bit) ((mask&(1LL<<bit))!=0)
#ifdef G
#define dg(x) cout<<#x<<' '<<x<<endl;
#define dg2(x,y) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<endl;
#define dg3(x,y,z) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<endl;
#define dg4(x,y,z,zz) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<' '<<#zz<<' '<<zz<<endl;
#else
#define dg(x) ;
#define dg2(x,y) ;
#define dg3(x,y,z) ;
#define dg4(x,y,z,zz) ;
#endif
#define ds(n,m) scanf("%lld %lld",&n,&m)
#define ts(n,m,k) scanf("%lld %lld %lld",&n,&m,&k)
#define yes printf("YES\n")
#define no printf("NO\n")
typedef long double ld;
typedef long long int lld;
using namespace std;
const lld M =1e3+10;
const lld mod=1e9+7;
const lld infi =LLONG_MAX;
lld ans[M],k,n,x,y,m,mymax=LLONG_MIN,mymin=LLONG_MAX,b,c,z,sum;
lld speed[12],visit[M][M][10],visit2[M][M];
char s[M][M];
queue<pll>pq[12],tpq[12];
lld mx[]={-1,1,0,0};
lld my[]={0,0,1,-1};
bool check(lld x,lld y,lld id,lld moves){
    if(x<0||x>=n)return false;
    if(y<0||y>=m)return false;
    if(visit2[x][y]==0)
        {
            return true;
        }
        else{
        if(visit2[x][y]!=id)return false;
        if(visit[x][y][id]>=moves)return false;
        return true;
    }
}
lld bfs(lld x,lld y,lld sp,lld pid){
    queue<pair<pll,lld>> tempq;
    tempq.push({{x,y},sp});
    lld i,j;
    while(!tempq.empty()){
        lld csp=tempq.front().S;
        lld x=tempq.front().F.F,y=tempq.front().F.S;
        tempq.pop();
        if(csp==0){
            tpq[pid].push({x,y});
            continue;
        }
        up(0,4,i){
            if(check(x+mx[i],y+my[i],pid,csp)){
                if(abs(mx[i])+abs(my[i])<=csp){
                    if(visit2[x+mx[i]][y+my[i]]==0)
                    ans[pid]++;
                    tempq.push({{x+mx[i],y+my[i]},csp-(abs(mx[i])+abs(my[i]))});
                    visit2[x+mx[i]][y+my[i]]=pid;
                    visit[x+mx[i]][y+my[i]][pid]=csp;
                }
            }
        }
    }

}
int main()
{
  lld i,j;
  lld p;
  ts(n,m,p);
  up(1,p+1,i)is(speed[i]);
  up(0,n,i){
    ss(s[i]);
    up(0,m,j){
        if(!(s[i][j]=='#'||s[i][j]=='.')){
            pq[s[i][j]-'0'].push({i,j});
            ans[s[i][j]-'0']++;
            visit2[i][j]=s[i][j]-'0';
        }
        else if(s[i][j]=='#'){
            visit2[i][j]=p+2;
        }
    }
  }
  lld cnt=0;
  do{
    cnt=0;
      up(1,p+1,i){
        if(pq[i].empty()){
            continue;
        }
        cnt++;
        while(!pq[i].empty()){
            bfs(pq[i].front().F,pq[i].front().S,speed[i],i);
            pq[i].pop();
        }
        while(!tpq[i].empty()){
            pq[i].push(tpq[i].front());
            tpq[i].pop();
        }
      }
    }while(cnt!=0);
  up(1,p+1,i)pps(ans[i]);
  ps("\n");
  return 0;
}


#include<bits/stdc++.h>
#define up(j,k,i) for(i=j;i<k;i++)
#define down(j,k,i) for(i=j;i>k;i--)
#define pp(n) printf("%lld\n",n)
#define pps(n) printf("%lld ",n)
#define all(x) x.begin(),x.end()
#define ps(s) printf("%s",s)
#define is(n) scanf("%lld",&n)
#define ips(n) scanf("%lld",n)
#define ss(s) scanf("%s",s)
#define cool 0
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i) cout<<i<<endl;
#define pll pair<lld,lld>
#define pi acos(-1)
#define isset(mask,bit) ((mask&(1LL<<bit))!=0)
#ifdef G
#define dg(x) cout<<#x<<' '<<x<<endl;
#define dg2(x,y) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<endl;
#define dg3(x,y,z) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<endl;
#define dg4(x,y,z,zz) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<' '<<#zz<<' '<<zz<<endl;
#else
#define dg(x) ;
#define dg2(x,y) ;
#define dg3(x,y,z) ;
#define dg4(x,y,z,zz) ;
#endif
#define ds(n,m) scanf("%lld %lld",&n,&m)
#define ts(n,m,k) scanf("%lld %lld %lld",&n,&m,&k)
#define yes printf("YES\n")
#define no printf("NO\n")
typedef long double ld;
typedef long long int lld;
using namespace std;
const lld M =2e3+10;
const lld mod=1e9+7;
const lld infi =LLONG_MAX;
lld ans[M],k,n,x,y,m,mymax=LLONG_MIN,mymin=LLONG_MAX,b,c,z,sum;
lld speed[12],visit[M][M][10],visit2[M][M];
char s[M][M];
queue<pll>pq[12],tpq[12];
lld mx[]={-1,1,0,0};
lld my[]={0,0,1,-1};
bool check(lld x,lld y,lld id,lld moves){
    if(x<0||x>=n)return false;
    if(y<0||y>=m)return false;
    if(visit2[x][y]==0)
        {
            return true;
        }
        else{
        if(visit2[x][y]!=id)return false;
        if(visit[x][y][id]>=moves)return false;
        return true;
    }
}
lld bfs(lld x,lld y,lld sp,lld pid){
    queue<pair<pll,lld>> tempq;
    tempq.push({{x,y},sp});
    lld i,j;
    while(!tempq.empty()){
        lld csp=tempq.front().S;
        lld x=tempq.front().F.F,y=tempq.front().F.S;
        tempq.pop();
        if(csp==0){
            tpq[pid].push({x,y});
            continue;
        }
        up(0,4,i){
            if(check(x+mx[i],y+my[i],pid,csp)){
                if(abs(mx[i])+abs(my[i])<=csp){
                    if(visit2[x+mx[i]][y+my[i]]==0)
                    ans[pid]++;
                    tempq.push({{x+mx[i],y+my[i]},csp-(abs(mx[i])+abs(my[i]))});
                    visit2[x+mx[i]][y+my[i]]=pid;
                    visit[x+mx[i]][y+my[i]][pid]=csp;
                }
            }
        }
    }

}
int main()
{
  lld i,j;
  lld p;
  ts(n,m,p);
  up(1,p+1,i)is(speed[i]);
  up(0,n,i){
    ss(s[i]);
    up(0,m,j){
        if(!(s[i][j]=='#'||s[i][j]=='.')){
            pq[s[i][j]-'0'].push({i,j});
            ans[s[i][j]-'0']++;
            visit2[i][j]=s[i][j]-'0';
        }
        else if(s[i][j]=='#'){
            visit2[i][j]=p+2;
        }
    }
  }
  lld cnt=0;
  do{
    cnt=0;
      up(1,p+1,i){
        if(pq[i].empty()){
            continue;
        }
        cnt++;
        while(!pq[i].empty()){
            bfs(pq[i].front().F,pq[i].front().S,speed[i],i);
            pq[i].pop();
        }
        while(!tpq[i].empty()){
            pq[i].push(tpq[i].front());
            tpq[i].pop();
        }
      }
    }while(cnt!=0);
  up(1,p+1,i)pps(ans[i]);
  ps("\n");
  return 0;
}


#include<bits/stdc++.h>
#define up(j,k,i) for(i=j;i<k;i++)
#define down(j,k,i) for(i=j;i>k;i--)
#define pp(n) printf("%lld\n",n)
#define pps(n) printf("%lld ",n)
#define all(x) x.begin(),x.end()
#define ps(s) printf("%s",s)
#define is(n) scanf("%lld",&n)
#define ips(n) scanf("%lld",n)
#define ss(s) scanf("%s",s)
#define cool 0
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i) cout<<i<<endl;
#define pll pair<lld,lld>
#define pi acos(-1)
#define isset(mask,bit) ((mask&(1LL<<bit))!=0)
#ifdef G
#define dg(x) cout<<#x<<' '<<x<<endl;
#define dg2(x,y) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<endl;
#define dg3(x,y,z) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<endl;
#define dg4(x,y,z,zz) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<' '<<#zz<<' '<<zz<<endl;
#else
#define dg(x) ;
#define dg2(x,y) ;
#define dg3(x,y,z) ;
#define dg4(x,y,z,zz) ;
#endif
#define ds(n,m) scanf("%lld %lld",&n,&m)
#define ts(n,m,k) scanf("%lld %lld %lld",&n,&m,&k)
#define yes printf("YES\n")
#define no printf("NO\n")
typedef long double ld;
typedef long long int lld;
using namespace std;
const lld M =1e3+10;
const lld mod=1e9+7;
const lld infi =LLONG_MAX;
lld ans[M],k,n,x,y,m,mymax=LLONG_MIN,mymin=LLONG_MAX,b,c,z,sum;
lld speed[12],visit[M][M];
char s[M][M];
queue<pll>pq[12],tpq[12];
lld mx[]={-1,1,0,0};
lld my[]={0,0,1,-1};
bool check(lld x,lld y,lld id,lld moves){
    if(x<0||x>=n)return false;
    if(y<0||y>=m)return false;
    if(visit[x][y]!=0)return false;
    if(s[x][y]!='.')return false;
        return true;
}
lld bfs(lld sp,lld pid){
    queue<pair<pll,lld>> tempq;
    while(!pq[pid].empty()){
        tempq.push({pq[pid].front(),sp});
        pq[pid].pop();
    }
    lld i,j;
    while(!tempq.empty()){
        lld csp=tempq.front().S;
        lld x=tempq.front().F.F,y=tempq.front().F.S;
        tempq.pop();
        if(csp==0){
            tpq[pid].push({x,y});
            continue;
        }
        up(0,4,i){
            if(check(x+mx[i],y+my[i],pid,csp)){
                if(abs(mx[i])+abs(my[i])<=csp){
                    ans[pid]++;
                    tempq.push({{x+mx[i],y+my[i]},csp-(abs(mx[i])+abs(my[i]))});
                    visit[x+mx[i]][y+my[i]]=pid;
                }
            }
        }
    }

}
int main()
{
  lld i,j;
  lld p;
  ts(n,m,p);
  up(1,p+1,i)is(speed[i]);
  up(0,n,i){
    ss(s[i]);
    up(0,m,j){
        if(!(s[i][j]=='#'||s[i][j]=='.')){
            pq[s[i][j]-'0'].push({i,j});
            ans[s[i][j]-'0']++;
            visit[i][j]=s[i][j]-'0';
        }
        else if(s[i][j]=='#'){
            visit[i][j]=p+2;
        }
    }
  }
  lld cnt=0;
  do{
    cnt=0;
      up(1,p+1,i){
        if(pq[i].empty()){
            continue;
        }
        cnt++;
        while(!pq[i].empty()){
            bfs(speed[i],i);
        }
        while(!tpq[i].empty()){
            pq[i].push(tpq[i].front());
            tpq[i].pop();
        }
      }
    }while(cnt!=0);
  up(1,p+1,i)pps(ans[i]);
  ps("\n");
  return 0;
}


#include<bits/stdc++.h>
#define up(j,k,i) for(i=j;i<k;i++)
#define down(j,k,i) for(i=j;i>k;i--)
#define pp(n) printf("%lld\n",n)
#define pps(n) printf("%lld ",n)
#define all(x) x.begin(),x.end()
#define ps(s) printf("%s",s)
#define is(n) scanf("%lld",&n)
#define ips(n) scanf("%lld",n)
#define ss(s) scanf("%s",s)
#define cool 0
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i) cout<<i<<endl;
#define pll pair<lld,lld>
#define pi acos(-1)
#define isset(mask,bit) ((mask&(1LL<<bit))!=0)
#ifdef G
#define dg(x) cout<<#x<<' '<<x<<endl;
#define dg2(x,y) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<endl;
#define dg3(x,y,z) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<endl;
#define dg4(x,y,z,zz) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<' '<<#zz<<' '<<zz<<endl;
#else
#define dg(x) ;
#define dg2(x,y) ;
#define dg3(x,y,z) ;
#define dg4(x,y,z,zz) ;
#endif
#define ds(n,m) scanf("%lld %lld",&n,&m)
#define ts(n,m,k) scanf("%lld %lld %lld",&n,&m,&k)
#define yes printf("YES\n")
#define no printf("NO\n")
typedef long double ld;
typedef long long int lld;
using namespace std;
const lld M =1e3+10;
const lld mod=1e9+7;
const lld infi =LLONG_MAX;
lld ans[M],k,n,x,y,m,mymax=LLONG_MIN,mymin=LLONG_MAX,b,c,z,sum;
vector<lld> v[M];
lld speed[11],visit[M][M];
char s[M][M];
queue<pll>pq[10],tpq[11];
lld mx[]={-1,1,0,0,1,1,-1,-1};
lld my[]={0,0,1,-1,1,-1,1,-1};
bool check(lld x,lld y){
    if(x<0||x>=n)return false;
    if(y<0||y>=m)return false;
    if(visit[x][y]!=0)return false;
    if(s[x][y]!='.')return false;
    return true;
}
lld bfs(lld x,lld y,lld sp,lld pid){
    queue<pair<pll,lld>> tempq;
    tempq.push({{x,y},sp});
    lld i,j;
    while(!tempq.empty()){
        lld csp=tempq.front().S;
        lld x=tempq.front().F.F,y=tempq.front().F.S;
        tempq.pop();
        if(csp==0){
            tpq[pid].push({x,y});
            continue;
        }
        up(0,8,i){
            if(check(x+mx[i],y+my[i])){
                if(abs(mx[i])+abs(my[i])<=csp){
                    visit[x+mx[i]][y+my[i]]=pid;
                    ans[pid]++;
                    tempq.push({{x+mx[i],y+my[i]},csp-(abs(mx[i])+abs(my[i]))});
                }
            }
        }
    }

}
int main()
{
  lld i,j;
  lld p;
  ts(n,m,p);
  up(1,p+1,i)is(speed[i]);
  up(0,n,i){
    ss(s[i]);
    up(0,m,j){
        if(!(s[i][j]=='#'||s[i][j]=='.')){
            pq[s[i][j]-'0'].push({i,j});
            ans[s[i][j]-'0']++;
            visit[i][j]=s[i][j]-'0';
        }
    }
  }
  lld cnt=0;
  do{
    cnt=0;
      up(1,p+1,i){
        if(pq[i].empty()){
            continue;
        }
        cnt++;
        while(!pq[i].empty()){
            bfs(pq[i].front().F,pq[i].front().S,speed[i],i);
            pq[i].pop();
        }
        while(!tpq[i].empty()){
            pq[i].push(tpq[i].front());
            tpq[i].pop();
        }
      }
    }while(cnt!=0);
  up(1,p+1,i)pps(ans[i]);
  ps("\n");
  return 0;
}


#include<bits/stdc++.h>
#define up(j,k,i) for(i=j;i<k;i++)
#define down(j,k,i) for(i=j;i>k;i--)
#define pp(n) printf("%lld\n",n)
#define pps(n) printf("%lld ",n)
#define all(x) x.begin(),x.end()
#define ps(s) printf("%s",s)
#define is(n) scanf("%lld",&n)
#define ips(n) scanf("%lld",n)
#define ss(s) scanf("%s",s)
#define cool 0
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i) cout<<i<<endl;
#define pll pair<lld,lld>
#define pi acos(-1)
#define isset(mask,bit) ((mask&(1LL<<bit))!=0)
#ifdef G
#define dg(x) cout<<#x<<' '<<x<<endl;
#define dg2(x,y) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<endl;
#define dg3(x,y,z) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<endl;
#define dg4(x,y,z,zz) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<' '<<#zz<<' '<<zz<<endl;
#else
#define dg(x) ;
#define dg2(x,y) ;
#define dg3(x,y,z) ;
#define dg4(x,y,z,zz) ;
#endif
#define ds(n,m) scanf("%lld %lld",&n,&m)
#define ts(n,m,k) scanf("%lld %lld %lld",&n,&m,&k)
#define yes printf("YES\n")
#define no printf("NO\n")
typedef long double ld;
typedef long long int lld;
using namespace std;
const lld M =1e3+10;
const lld mod=1e9+7;
const lld infi =LLONG_MAX;
lld ans[M],k,n,x,y,m,mymax=LLONG_MIN,mymin=LLONG_MAX,b,c,z,sum;
lld speed[12],visit[M][M][10],visit2[M][M];
char s[M][M];
queue<pll>pq[12],tpq[12];
lld mx[]={-1,1,0,0};
lld my[]={0,0,1,-1};
bool check(lld x,lld y,lld id,lld moves){
    if(x<0||x>=n)return false;
    if(y<0||y>=m)return false;
    if(visit2[x][y]==0)
        {
            return true;
        }
        else{
        if(visit2[x][y]!=id)return false;
        if(visit[x][y][id]>=moves)return false;
        return true;
    }
}
lld present[M][M];
lld bfs(lld x,lld y,lld sp,lld pid){
    queue<pair<pll,lld>> tempq;
    tempq.push({{x,y},sp});
    lld i,j;
    while(!tempq.empty()){
        lld csp=tempq.front().S;
        lld x=tempq.front().F.F,y=tempq.front().F.S;
        tempq.pop();
        // if(visit[x][y][pid]<csp+1)
        //     continue;
        if(csp==0){
            if(present[x][y]==0){
            tpq[pid].push({x,y});
            present[x][y]=1;
        }
            continue;
        }
        up(0,4,i){
            if(check(x+mx[i],y+my[i],pid,csp)){
                if(abs(mx[i])+abs(my[i])<=csp){
                    if(visit2[x+mx[i]][y+my[i]]==0)
                    ans[pid]++;
                    tempq.push({{x+mx[i],y+my[i]},csp-(abs(mx[i])+abs(my[i]))});
                    visit2[x+mx[i]][y+my[i]]=pid;
                    visit[x+mx[i]][y+my[i]][pid]=csp;
                }
            }
        }
    }

}
int main()
{
  lld i,j;
  lld p;
  ts(n,m,p);
  up(1,p+1,i)is(speed[i]);
  up(0,n,i){
    ss(s[i]);
    up(0,m,j){
        if(!(s[i][j]=='#'||s[i][j]=='.')){
            pq[s[i][j]-'0'].push({i,j});
            ans[s[i][j]-'0']++;
            visit2[i][j]=s[i][j]-'0';
        }
        else if(s[i][j]=='#'){
            visit2[i][j]=p+2;
        }
    }
  }
  lld cnt=0;
  do{
    cnt=0;
      up(1,p+1,i){
        if(pq[i].empty()){
            continue;
        }
        cnt++;
        while(!pq[i].empty()){
            bfs(pq[i].front().F,pq[i].front().S,speed[i],i);
            pq[i].pop();
        }
        while(!tpq[i].empty()){
            pq[i].push(tpq[i].front());
            tpq[i].pop();
        }
      }
    }while(cnt!=0);
  up(1,p+1,i)pps(ans[i]);
  ps("\n");
  return 0;
}


#include<bits/stdc++.h>
#define up(j,k,i) for(i=j;i<k;i++)
#define down(j,k,i) for(i=j;i>k;i--)
#define pp(n) printf("%lld\n",n)
#define pps(n) printf("%lld ",n)
#define all(x) x.begin(),x.end()
#define ps(s) printf("%s",s)
#define is(n) scanf("%lld",&n)
#define ips(n) scanf("%lld",n)
#define ss(s) scanf("%s",s)
#define cool 0
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i) cout<<i<<endl;
#define pll pair<lld,lld>
#define pi acos(-1)
#define isset(mask,bit) ((mask&(1LL<<bit))!=0)
#ifdef G
#define dg(x) cout<<#x<<' '<<x<<endl;
#define dg2(x,y) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<endl;
#define dg3(x,y,z) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<endl;
#define dg4(x,y,z,zz) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<' '<<#zz<<' '<<zz<<endl;
#else
#define dg(x) ;
#define dg2(x,y) ;
#define dg3(x,y,z) ;
#define dg4(x,y,z,zz) ;
#endif
#define ds(n,m) scanf("%lld %lld",&n,&m)
#define ts(n,m,k) scanf("%lld %lld %lld",&n,&m,&k)
#define yes printf("YES\n")
#define no printf("NO\n")
typedef long double ld;
typedef long long int lld;
using namespace std;
const lld M =1e3+10;
const lld mod=1e9+7;
const lld infi =LLONG_MAX;
lld ans[M],k,n,x,y,m,mymax=LLONG_MIN,mymin=LLONG_MAX,b,c,z,sum;
lld speed[12],visit[M][M][10],visit2[M][M];
char s[M][M];
queue<pll>pq[12],tpq[12];
lld mx[]={-1,1,0,0};
lld my[]={0,0,1,-1};
bool check(lld x,lld y,lld id,lld moves){
    if(x<0||x>=n)return false;
    if(y<0||y>=m)return false;
    if(visit2[x][y]==0)
        {
            return true;
        }
        else{
        if(visit2[x][y]!=id)return false;
        if(visit[x][y][id]>=moves)return false;
        return true;
    }
}
lld present[M][M];
lld bfs(lld x,lld y,lld sp,lld pid){
    queue<pair<pll,lld>> tempq;
    tempq.push({{x,y},sp});
    lld i,j;
    while(!tempq.empty()){
        lld csp=tempq.front().S;
        lld x=tempq.front().F.F,y=tempq.front().F.S;
        tempq.pop();
        if(visit[x][y][pid]>csp+1)
            continue;
        if(csp==0){
            if(present[x][y]==0){
            tpq[pid].push({x,y});
            present[x][y]=1;
        }
            continue;
        }
        up(0,4,i){
            if(check(x+mx[i],y+my[i],pid,csp)){
                if(abs(mx[i])+abs(my[i])<=csp){
                    if(visit2[x+mx[i]][y+my[i]]==0)
                    ans[pid]++;
                    tempq.push({{x+mx[i],y+my[i]},csp-(abs(mx[i])+abs(my[i]))});
                    visit2[x+mx[i]][y+my[i]]=pid;
                    visit[x+mx[i]][y+my[i]][pid]=csp;
                }
            }
        }
    }

}
int main()
{
  lld i,j;
  lld p;
  ts(n,m,p);
  up(1,p+1,i)is(speed[i]);
  up(0,n,i){
    ss(s[i]);
    up(0,m,j){
        if(!(s[i][j]=='#'||s[i][j]=='.')){
            pq[s[i][j]-'0'].push({i,j});
            ans[s[i][j]-'0']++;
            visit2[i][j]=s[i][j]-'0';
        }
        else if(s[i][j]=='#'){
            visit2[i][j]=p+2;
        }
    }
  }
  lld cnt=0;
  do{
    cnt=0;
      up(1,p+1,i){
        if(pq[i].empty()){
            continue;
        }
        cnt++;
        while(!pq[i].empty()){
            bfs(pq[i].front().F,pq[i].front().S,speed[i],i);
            pq[i].pop();
        }
        while(!tpq[i].empty()){
            pq[i].push(tpq[i].front());
            tpq[i].pop();
        }
      }
    }while(cnt!=0);
  up(1,p+1,i)pps(ans[i]);
  ps("\n");
  return 0;
}


#include<bits/stdc++.h>
#define up(j,k,i) for(i=j;i<k;i++)
#define down(j,k,i) for(i=j;i>k;i--)
#define pp(n) printf("%lld\n",n)
#define pps(n) printf("%lld ",n)
#define all(x) x.begin(),x.end()
#define ps(s) printf("%s",s)
#define is(n) scanf("%lld",&n)
#define ips(n) scanf("%lld",n)
#define ss(s) scanf("%s",s)
#define cool 0
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i) cout<<i<<endl;
#define pll pair<lld,lld>
#define pi acos(-1)
#define isset(mask,bit) ((mask&(1LL<<bit))!=0)
#ifdef G
#define dg(x) cout<<#x<<' '<<x<<endl;
#define dg2(x,y) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<endl;
#define dg3(x,y,z) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<endl;
#define dg4(x,y,z,zz) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<' '<<#zz<<' '<<zz<<endl;
#else
#define dg(x) ;
#define dg2(x,y) ;
#define dg3(x,y,z) ;
#define dg4(x,y,z,zz) ;
#endif
#define ds(n,m) scanf("%lld %lld",&n,&m)
#define ts(n,m,k) scanf("%lld %lld %lld",&n,&m,&k)
#define yes printf("YES\n")
#define no printf("NO\n")
typedef long double ld;
typedef long long int lld;
using namespace std;
const lld M =1e3+10;
const lld mod=1e9+7;
const lld infi =LLONG_MAX;
lld ans[M],k,n,x,y,m,mymax=LLONG_MIN,mymin=LLONG_MAX,b,c,z,sum;
vector<lld> v[M];
lld speed[11],visit[M][M];
char s[M][M];
queue<pll>pq[10],tpq[11];
lld mx[]={-1,1,0,0,1,1,-1,-1};
lld my[]={0,0,1,-1,1,-1,1,-1};
bool check(lld x,lld y){
    if(x<0||x>=n)return false;
    if(y<0||y>=m)return false;
    if(visit[x][y]!=0)return false;
    if(s[x][y]!='.')return false;
    return true;
}
lld bfs(lld x,lld y,lld sp,lld pid){
    queue<pair<pll,lld>> tempq;
    tempq.push({{x,y},sp});
    lld i,j;
    while(!tempq.empty()){
        lld csp=tempq.front().S;
        lld x=tempq.front().F.F,y=tempq.front().F.S;
        tempq.pop();
        if(csp==0){
            tpq[pid].push({x,y});
            continue;
        }
        up(0,8,i){
            if(check(x+mx[i],y+my[i])){
                if(abs(mx[i])+abs(my[i])<=csp){
                    visit[x+mx[i]][y+my[i]]=pid;
                    ans[pid]++;
                    tempq.push({{x+mx[i],y+my[i]},csp-1});
                }
            }
        }
    }

}
int main()
{
  lld i,j;
  lld p;
  ts(n,m,p);
  up(1,p+1,i)is(speed[i]);
  up(0,n,i){
    ss(s[i]);
    up(0,m,j){
        if(!(s[i][j]=='#'||s[i][j]=='.')){
            pq[s[i][j]-'0'].push({i,j});
            ans[s[i][j]-'0']++;
            visit[i][j]=s[i][j]-'0';
        }
    }
  }
  lld cnt=0;
  do{
    cnt=0;
      up(1,p+1,i){
        if(pq[i].empty()){
            continue;
        }
        cnt++;
        while(!pq[i].empty()){
            bfs(pq[i].front().F,pq[i].front().S,speed[i],i);
            pq[i].pop();
        }
        while(!tpq[i].empty()){
            pq[i].push(tpq[i].front());
            tpq[i].pop();
        }
      }
    }while(cnt!=0);
  up(1,p+1,i)pps(ans[i]);
  ps("\n");
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, int& count);

int n = 4, m = 4, p = 2;

int main() {

	scanf("%d%d%d", &n, &m, &p);
	vector<size_t> speed(p, 0);
	//vector<size_t> speed = { 1, 1000000000 };
	vector<vector<char>> grid(n, vector<char>(m, '.'));
	//vector<vector<char>> grid = {
	//	{'.','.','.', '.'},
	//	{'.','.','.', '.'},
	//{'.','.','2','.'},
	//{'.','.','.','1'}
	//};
	size_t num = 0;
	char c;
	vector<queue<pair<int, int>>> lp(p);

	////
	//lp[0].push(pair<int, int>{3, 3});
	//lp[1].push(pair<int, int>{2, 2});
	//lp[2].push(pair<int, int>{2, 2});
	//lp[3].push(pair<int, int>{2, 3});
	////

	vector<size_t> nums(p, 1);
	pair<int, int> pr;
	int x;
	int y;
	int key = 1;
	int si;
	int count = 0;

	for (int i = 0; i < p; ++i)
	{
		scanf("%d", &num);
		speed[i] = num;
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			cin >> c;
			grid[i][j] = c;
			if (c != '.' && c != '#')
			{
				num = c - '0';
				lp[num - 1].push(pair<int, int>{ i, j });
			}
			if (c == '.')
				++count;
		}
	}

	while (key)
	{
		if (count == 0)
			break;
		key = 0;
		for (size_t i = 0; i < p; ++i)
		{
			if (count == 0)
				break;
			si = speed[i];
			while (si)
			{
				if (count == 0)
					break;
				num = lp[i].size();
				while (num)
				{
					pr = lp[i].front();
					lp[i].pop();
					x = pr.first;
					y = pr.second;
					BFS(grid, nums, lp, i, x, y, count);

					--num;
				}
				--si;
			}

			if (!lp[i].empty())
			{
				key = 1;
				continue;
			}

		}
	}

	for (int i = 0; i < p; ++i)
	{
		cout << nums[i] << " ";
	}

	cout << endl;

	return 0;
}

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, int& count)
{
	if (x - 1 >= 0)
	{
		if (grid[x - 1][y] == '.')
		{
			--count;
			grid[x - 1][y] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x - 1, y});
		}
	}
	if (x + 1 < n)
	{
		if (grid[x + 1][y] == '.')
		{
			--count;
			grid[x + 1][y] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x + 1, y});
		}
	}
	if (y - 1 >= 0)
	{
		if (grid[x][y - 1] == '.')
		{
			--count;
			grid[x][y - 1] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x, y - 1});
		}
	}
	if (y + 1 < m)
	{
		if (grid[x][y + 1] == '.')
		{
			--count;
			grid[x][y + 1] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x, y + 1});
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, int& count);

int n = 4, m = 4, p = 2;

int main() {

	scanf("%d%d%d", &n, &m, &p);
	vector<size_t> speed(p, 0);
	//vector<size_t> speed = { 1, 1000000000 };
	vector<vector<char>> grid(n, vector<char>(m, '.'));
	//vector<vector<char>> grid = {
	//	{'.','.','.', '.'},
	//	{'.','.','.', '.'},
	//{'.','.','2','.'},
	//{'.','.','.','1'}
	//};
	size_t num = 0;
	char c;
	vector<queue<pair<int, int>>> lp(p);

	////
	//lp[0].push(pair<int, int>{3, 3});
	//lp[1].push(pair<int, int>{2, 2});
	//lp[2].push(pair<int, int>{2, 2});
	//lp[3].push(pair<int, int>{2, 3});
	////

	vector<size_t> nums(p, 1);
	pair<int, int> pr;
	int x;
	int y;
	int key = 1;
	int si;
	int count = 0;

	for (int i = 0; i < p; ++i)
	{
		scanf("%d", &num);
		speed[i] = num;
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			cin >> c;
			grid[i][j] = c;
			if (c != '.' && c != '#')
			{
				num = c - '0';
				lp[num - 1].push(pair<int, int>{ i, j });
			}
			if (c == '.')
				++count;
		}
	}

	while (key)
	{
		if (count == 0)
			break;
		key = 0;
		for (size_t i = 0; i < p; ++i)
		{
			if (count == 0)
				break;
			si = speed[i];
			while (si)
			{
				if (count == 0)
					break;
				num = lp[i].size();
				if (num == 0)
					break;
				while (num)
				{
					pr = lp[i].front();
					lp[i].pop();
					x = pr.first;
					y = pr.second;
					BFS(grid, nums, lp, i, x, y, count);

					--num;
				}
				--si;
			}

			if (!lp[i].empty())
			{
				key = 1;
				continue;
			}

		}
	}

	for (int i = 0; i < p; ++i)
	{
		cout << nums[i] << " ";
	}

	cout << endl;

	return 0;
}

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, int& count)
{
	if (x - 1 >= 0)
	{
		if (grid[x - 1][y] == '.')
		{
			--count;
			grid[x - 1][y] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x - 1, y});
		}
	}
	if (x + 1 < n)
	{
		if (grid[x + 1][y] == '.')
		{
			--count;
			grid[x + 1][y] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x + 1, y});
		}
	}
	if (y - 1 >= 0)
	{
		if (grid[x][y - 1] == '.')
		{
			--count;
			grid[x][y - 1] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x, y - 1});
		}
	}
	if (y + 1 < m)
	{
		if (grid[x][y + 1] == '.')
		{
			--count;
			grid[x][y + 1] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x, y + 1});
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y);

int n = 3, m = 3, p = 2;

int main() {

	scanf("%d%d%d", &n, &m, &p);
	vector<size_t> speed(p, 0);
	//vector<size_t> speed = { 1, 1};
	vector<vector<char>> grid(n, vector<char>(m, '.'));
	//vector<vector<char>> grid = {
	//	{'1','.','.'},
	//{'.','.','.'},
	//{'.','.','2'}
	//};
	size_t num = 0;
	char c;
	vector<queue<pair<int, int>>> lp(p);

	////
	//lp[0].push(pair<int, int>{0, 0});
	//lp[1].push(pair<int, int>{2, 2});
	//lp[2].push(pair<int, int>{2, 2});
	//lp[3].push(pair<int, int>{2, 3});
	////

	vector<size_t> nums(p, 1);
	pair<int, int> pr;
	int x;
	int y;
	int key = 1;
	int si;

	for (int i = 0; i < p; ++i)
	{
		scanf("%d", &num);
		speed[i] = num;
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			cin >> c;
			grid[i][j] = c;
			if (c != '.' && c != '#')
			{
				num = c - '0';
				lp[num - 1].push(pair<int, int>{ i, j });
			}
		}
	}

	while (key)
	{
		key = 0;
		for (size_t i = 0; i < p; ++i)
		{
			si = speed[i];
			while (si)
			{
				num = lp[i].size();
				while (num)
				{
					pr = lp[i].front();
					lp[i].pop();
					x = pr.first;
					y = pr.second;
					BFS(grid, nums, lp, i, x, y);

					--num;
				}
				--si;
			}

			if (!lp[i].empty())
			{
				key = 1;
				continue;
			}

		}
	}

	for (int i = 0; i < p; ++i)
	{
		cout << nums[i] << " ";
	}

	cout << endl;

	return 0;
}

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y)
{
	if (x - 1 >= 0)
	{
		if (grid[x - 1][y] == '.')
		{
			grid[x - 1][y] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x - 1, y});
		}
	}
	if (x + 1 < n)
	{
		if (grid[x + 1][y] == '.')
		{
			grid[x + 1][y] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x + 1, y});
		}
	}
	if (y - 1 >= 0)
	{
		if (grid[x][y - 1] == '.')
		{
			grid[x][y - 1] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x, y - 1});
		}
	}
	if (y + 1 < m)
	{
		if (grid[x][y + 1] == '.')
		{
			grid[x][y + 1] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x, y + 1});
		}
	}
}


#include <bits/stdc++.h>
using namespace std;

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, bool ju);

int n, m, p;

int main() {

	scanf("%d%d%d", &n, &m, &p);
	vector<size_t> speed(p, 0);
	//vector<size_t> speed = { 1, 1, 1, 1 };
	vector<vector<char>> grid(n, vector<char>(m, '.'));
	//vector<vector<char>> grid = {
	//	{'.','.','.', '.'},
	//{'#','.','.','.'},
	//{'1','2','3','4'}
	//};
	size_t num = 0;
	char c;
	vector<queue<pair<int, int>>> lp(p);

	////
	//lp[0].push(pair<int, int>{2, 0});
	//lp[1].push(pair<int, int>{2, 1});
	//lp[2].push(pair<int, int>{2, 2});
	//lp[3].push(pair<int, int>{2, 3});
	////

	vector<size_t> nums(p, 1);
	pair<int, int> pr;
	int x;
	int y;
	int key = 1;
	int si;

	for (int i = 0; i < p; ++i)
	{
		scanf("%d", &num);
		speed[i] = num;
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			scanf("%c", &c);
			grid[i][j] = c;
			if (c != '.' && c != '#')
			{
				num = c - '0';
				lp[num - 1].push(pair<int, int>{ x, y });
			}
		}
	}

	while (key)
	{
		key = 0;
		for (size_t i = 0; i < p; ++i)
		{
			num = lp[i].size();
			while(num)
			{
				pr = lp[i].front();
				lp[i].pop();
				x = pr.first;
				y = pr.second;
				si = 1;
				while (si <= speed[i])
				{
					if (si == speed[i])
					{
						BFS(grid, nums, lp, i, x, y, true);
					}
					else
					{
						BFS(grid, nums, lp, i, x, y, false);
					}
					++si;
				}
				--num;
			}

			if (!lp[i].empty())
			{
				key = 1;
				continue;
			}

		}
	}

	for (int i = 0; i < p; ++i)
	{
		cout << nums[i] << " ";
	}

	cout << endl;

	return 0;
}

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, bool ju)
{
	if (x - 1 >= 0)
	{
		if (grid[x - 1][y] == '.')
		{
			grid[x - 1][y] = i + '1';
			nums[i]++;
			if(ju)
				lp[i].push(pair<int, int>{x - 1, y});
		}
	}
	if (x + 1 < n)
	{
		if (grid[x + 1][y] == '.')
		{
			grid[x + 1][y] = i + '1';
			nums[i]++;
			if (ju)
				lp[i].push(pair<int, int>{x + 1, y});
		}
	}
	if (y - 1 >= 0)
	{
		if (grid[x][y - 1] == '.')
		{
			grid[x][y - 1] = i + '1';
			nums[i]++;
			if (ju)
				lp[i].push(pair<int, int>{x, y - 1});
		}
	}
	if (y + 1 < m)
	{
		if (grid[x][y + 1] == '.')
		{
			grid[x][y + 1] = i + '1';
			nums[i]++;
			if (ju)
				lp[i].push(pair<int, int>{x, y + 1});
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, int& count);

int n = 4, m = 4, p = 2;

int main() {

	scanf("%d%d%d", &n, &m, &p);
	vector<size_t> speed(p, 0);
	//vector<size_t> speed = { 1, 1000000000 };
	vector<vector<char>> grid(n, vector<char>(m, '.'));
	//vector<vector<char>> grid = {
	//	{'.','.','.', '.'},
	//	{'.','.','.', '.'},
	//{'.','.','2','.'},
	//{'.','.','.','1'}
	//};
	size_t num = 0;
	char c;
	vector<queue<pair<int, int>>> lp(p);

	////
	//lp[0].push(pair<int, int>{3, 3});
	//lp[1].push(pair<int, int>{2, 2});
	//lp[2].push(pair<int, int>{2, 2});
	//lp[3].push(pair<int, int>{2, 3});
	////

	vector<size_t> nums(p, 1);
	pair<int, int> pr;
	int x;
	int y;
	int key = 1;
	int si;
	int count = 14;

	for (int i = 0; i < p; ++i)
	{
		scanf("%d", &num);
		speed[i] = num;
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			cin >> c;
			grid[i][j] = c;
			if (c != '.' && c != '#')
			{
				num = c - '0';
				lp[num - 1].push(pair<int, int>{ i, j });
			}
			if (c == '.')
				++count;
		}
	}

	while (key)
	{
		if (count == 0)
			break;
		key = 0;
		for (size_t i = 0; i < p; ++i)
		{
			if (count == 0)
				break;
			si = speed[i];
			while (si)
			{
				if (count == 0)
					break;
				num = lp[i].size();
				while (num)
				{
					pr = lp[i].front();
					lp[i].pop();
					x = pr.first;
					y = pr.second;
					BFS(grid, nums, lp, i, x, y, count);

					--num;
				}
				--si;
			}

			if (!lp[i].empty())
			{
				key = 1;
				continue;
			}

		}
	}

	for (int i = 0; i < p; ++i)
	{
		cout << nums[i] << " ";
	}

	cout << endl;

	return 0;
}

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, int& count)
{
	if (x - 1 >= 0)
	{
		if (grid[x - 1][y] == '.')
		{
			--count;
			grid[x - 1][y] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x - 1, y});
		}
	}
	if (x + 1 < n)
	{
		if (grid[x + 1][y] == '.')
		{
			--count;
			grid[x + 1][y] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x + 1, y});
		}
	}
	if (y - 1 >= 0)
	{
		if (grid[x][y - 1] == '.')
		{
			--count;
			grid[x][y - 1] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x, y - 1});
		}
	}
	if (y + 1 < m)
	{
		if (grid[x][y + 1] == '.')
		{
			--count;
			grid[x][y + 1] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x, y + 1});
		}
	}
}


#include <bits/stdc++.h>
using namespace std;

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, bool ju);

int n, m, p;

int main() {

	scanf("%d%d%d", &n, &m, &p);
	vector<size_t> speed(p, 0);
	//vector<size_t> speed = { 1, 1, 1, 1 };
	vector<vector<char>> grid(n, vector<char>(m, '.'));
	//vector<vector<char>> grid = {
	//	{'.','.','.', '.'},
	//{'#','.','.','.'},
	//{'1','2','3','4'}
	//};
	size_t num = 0;
	char c;
	vector<queue<pair<int, int>>> lp(p);

	////
	//lp[0].push(pair<int, int>{2, 0});
	//lp[1].push(pair<int, int>{2, 1});
	//lp[2].push(pair<int, int>{2, 2});
	//lp[3].push(pair<int, int>{2, 3});
	////

	vector<size_t> nums(p, 1);
	pair<int, int> pr;
	int x;
	int y;
	int key = 1;
	int si;

	for (int i = 0; i < p; ++i)
	{
		scanf("%d", &num);
		speed[i] = num;
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
						cin >> c;
			grid[i][j] = c;
			if (c != '.' && c != '#')
			{
				num = c - '0';
				lp[num - 1].push(pair<int, int>{ i, j });
			}
		}
	}

	while (key)
	{
		key = 0;
		for (size_t i = 0; i < p; ++i)
		{
			num = lp[i].size();
			while(num)
			{
				pr = lp[i].front();
				lp[i].pop();
				x = pr.first;
				y = pr.second;
				si = 1;
				while (si <= speed[i])
				{
					if (si == speed[i])
					{
						BFS(grid, nums, lp, i, x, y, true);
					}
					else
					{
						BFS(grid, nums, lp, i, x, y, false);
					}
					++si;
				}
				--num;
			}

			if (!lp[i].empty())
			{
				key = 1;
				continue;
			}

		}
	}

	for (int i = 0; i < p; ++i)
	{
		cout << nums[i] << " ";
	}

	cout << endl;

	return 0;
}

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, bool ju)
{
	if (x - 1 >= 0)
	{
		if (grid[x - 1][y] == '.')
		{
			grid[x - 1][y] = i + '1';
			nums[i]++;
			if(ju)
				lp[i].push(pair<int, int>{x - 1, y});
		}
	}
	if (x + 1 < n)
	{
		if (grid[x + 1][y] == '.')
		{
			grid[x + 1][y] = i + '1';
			nums[i]++;
			if (ju)
				lp[i].push(pair<int, int>{x + 1, y});
		}
	}
	if (y - 1 >= 0)
	{
		if (grid[x][y - 1] == '.')
		{
			grid[x][y - 1] = i + '1';
			nums[i]++;
			if (ju)
				lp[i].push(pair<int, int>{x, y - 1});
		}
	}
	if (y + 1 < m)
	{
		if (grid[x][y + 1] == '.')
		{
			grid[x][y + 1] = i + '1';
			nums[i]++;
			if (ju)
				lp[i].push(pair<int, int>{x, y + 1});
		}
	}
}


#include<cstdio>
#include<iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, int& count);

int n = 3, m = 8, p = 3;

int main() {

	scanf("%d%d%d", &n, &m, &p);
	vector<size_t> speed(p, 0);
	//vector<size_t> speed = { 1, 1000000000 };
	vector<vector<char>> grid(n, vector<char>(m, '.'));
	//vector<vector<char>> grid = {
	//	{'.','2','2','2','.','3','2','1'},
	//	{'2','.','#','#','3','.','3','1'},
	//{'.','2','2','2','.','3','2','1'},
	//};
	size_t num = 0;
	char c;
	vector<queue<pair<int, int>>> lp(p);

	////
	//lp[0].push(pair<int, int>{2, 2});
	//lp[1].push(pair<int, int>{3,3});
	//lp[2].push(pair<int, int>{2, 2});
	//lp[3].push(pair<int, int>{2, 3});
	////

	vector<size_t> nums(p, 0);
	pair<int, int> pr;
	int x;
	int y;
	int key = 1;
	int si;
	int count = 0;

	for (int i = 0; i < p; ++i)
	{
		scanf("%d", &num);
		speed[i] = num;
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			cin >> c;
			grid[i][j] = c;
			if (c != '.' && c != '#')
			{
				num = c - '0';
				lp[num - 1].push(pair<int, int>{ i, j });
				nums[num - 1]++;
			}
			if (c == '.')
				++count;
		}
	}

	while (key)
	{
		if (count == 0)
			break;
		key = 0;
		for (size_t i = 0; i < p; ++i)
		{
			if (count == 0)
				break;
			si = speed[i];
			while (si)
			{
				if (count == 0)
					break;
				num = lp[i].size();
				if (num == 0)
					break;
				while (num)
				{
					pr = lp[i].front();
					lp[i].pop();
					x = pr.first;
					y = pr.second;
					BFS(grid, nums, lp, i, x, y, count);

					--num;
				}
				--si;
			}

			if (!lp[i].empty())
			{
				key = 1;
				continue;
			}

		}
	}

	for (int i = 0; i < p; ++i)
	{
		cout << nums[i] << " ";
	}

	cout << endl;

	return 0;
}

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, int& count)
{
	if (x - 1 >= 0)
	{
		if (grid[x - 1][y] == '.')
		{
			--count;
			grid[x - 1][y] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x - 1, y});
		}
	}
	if (x + 1 < n)
	{
		if (grid[x + 1][y] == '.')
		{
			--count;
			grid[x + 1][y] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x + 1, y});
		}
	}
	if (y - 1 >= 0)
	{
		if (grid[x][y - 1] == '.')
		{
			--count;
			grid[x][y - 1] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x, y - 1});
		}
	}
	if (y + 1 < m)
	{
		if (grid[x][y + 1] == '.')
		{
			--count;
			grid[x][y + 1] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x, y + 1});
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, int& count);

int n = 4, m = 4, p = 2;

int main() {

	scanf("%d%d%d", &n, &m, &p);
	vector<size_t> speed(p, 0);
	//vector<size_t> speed = { 1, 1000000000 };
	vector<vector<char>> grid(n, vector<char>(m, '.'));
	//vector<vector<char>> grid = {
	//	{'.','.','.', '.'},
	//	{'.','.','.', '.'},
	//{'.','.','2','.'},
	//{'.','.','.','1'}
	//};
	size_t num = 0;
	char c;
	vector<queue<pair<int, int>>> lp(p);

	////
	//lp[0].push(pair<int, int>{3, 3});
	//lp[1].push(pair<int, int>{2, 2});
	//lp[2].push(pair<int, int>{2, 2});
	//lp[3].push(pair<int, int>{2, 3});
	////

	vector<size_t> nums(p, 1);
	pair<int, int> pr;
	int x;
	int y;
	int key = 1;
	int si;
	int count = 14;

	for (int i = 0; i < p; ++i)
	{
		scanf("%d", &num);
		speed[i] = num;
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			cin >> c;
			grid[i][j] = c;
			if (c != '.' && c != '#')
			{
				num = c - '0';
				lp[num - 1].push(pair<int, int>{ i, j });
			}
			if (c == '.')
				++count;
		}
	}

	while (key)
	{
		if (count == 0)
			break;
		key = 0;
		for (size_t i = 0; i < p; ++i)
		{
			if (count == 0)
				break;
			si = speed[i];
			while (si)
			{
				if (count == 0)
					break;
				num = lp[i].size();
				while (num)
				{
					pr = lp[i].front();
					lp[i].pop();
					x = pr.first;
					y = pr.second;
					BFS(grid, nums, lp, i, x, y, count);

					--num;
				}
				--si;
			}

			if (!lp[i].empty())
			{
				key = 1;
				continue;
			}

		}
	}

	for (int i = 0; i < p; ++i)
	{
		cout << nums[i] << " ";
	}

	cout << endl;

	system("pause");
	return 0;
}

void BFS(vector<vector<char>>& grid, vector<size_t>& nums, vector<queue<pair<int, int>>>& lp, const int& i, const int& x, const int& y, int& count)
{
	if (x - 1 >= 0)
	{
		if (grid[x - 1][y] == '.')
		{
			--count;
			grid[x - 1][y] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x - 1, y});
		}
	}
	if (x + 1 < n)
	{
		if (grid[x + 1][y] == '.')
		{
			--count;
			grid[x + 1][y] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x + 1, y});
		}
	}
	if (y - 1 >= 0)
	{
		if (grid[x][y - 1] == '.')
		{
			--count;
			grid[x][y - 1] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x, y - 1});
		}
	}
	if (y + 1 < m)
	{
		if (grid[x][y + 1] == '.')
		{
			--count;
			grid[x][y + 1] = i + '1';
			nums[i]++;
			lp[i].push(pair<int, int>{x, y + 1});
		}
	}
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head

const int N = 1e3+10;
int n, m, p;
int a[N], vis[N][N];
int ans[20];
char s[N][N];
const int dx[]={1,-1,0,-0};
const int dy[]={0,0,1,-1};
struct _ {
	int x,y,id;
};
queue<_> q;

void dfs(int x, int y, int v, int id) {
	if (x<=0||x>n||y<=0||y>m||vis[x][y]||s[x][y]=='#') return;
	vis[x][y] = id;
	q.push({x,y,id});
	if (!v) return;
	REP(i,0,3) dfs(x+dx[i],y+dy[i],v-1,id);
}

int main() {
	scanf("%d%d%d", &n, &m, &p);
	REP(i,1,p) scanf("%d",a+i);
	REP(i,1,n) scanf("%s",s[i]+1);
	REP(ii,1,p) REP(i,1,n) REP(j,1,m) {
		if (s[i][j]-'0'==ii) {
			q.push({i,j,ii});
			vis[i][j]=ii;
		}
	}
	while (!q.empty()) {
		int x=q.front().x,y=q.front().y,id=q.front().id;
		q.pop();
		REP(i,0,3) dfs(x+dx[i],y+dy[i],a[id]-1,id);
	}
	REP(i,1,n) REP(j,1,m) ++ans[vis[i][j]];
	REP(i,1,p) printf("%d ",ans[i]);
	puts("");
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head

const int N = 1e3+10;
int n, m, p;
int a[N], vis[N][N];
int ans[20];
char s[N][N];
const int dx[]={1,-1,0,-0};
const int dy[]={0,0,1,-1};
struct _ {
	int x,y,id,v;
};

int main() {
	scanf("%d%d%d", &n, &m, &p);
	REP(i,1,p) scanf("%d",a+i);
	REP(i,1,n) scanf("%s",s[i]+1);
	queue<_> q;
	REP(ii,1,p) REP(i,1,n) REP(j,1,m) {
		if (s[i][j]-'0'==ii) {
			q.push({i,j,ii,a[ii]});
			vis[i][j]=ii;
		}
	}
	while (!q.empty()) {
		int x=q.front().x,y=q.front().y,id=q.front().id;q.pop();
		if (x<=0||x>n||y<=0||y>m||s[x][y]=='#') continue;
		queue<_> Q;
		REP(i,0,3) Q.push({x+dx[i],y+dy[i],id,a[id]-1});
		while (!Q.empty()) {
			int x=Q.front().x,y=Q.front().y,id=Q.front().id,v=Q.front().v;
			if (x<=0||x>n||y<=0||y>m||vis[x][y]||s[x][y]=='#') {
				Q.pop();
				continue;
			}
			q.push(Q.front());Q.pop();
			vis[x][y] = id;
			if (!v) continue;
			REP(i,0,3) Q.push({x+dx[i],y+dy[i],id,v-1});
		}
	}
	REP(i,1,n) REP(j,1,m) ++ans[vis[i][j]];
	REP(i,1,p) printf("%d ",ans[i]);
	puts("");
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3+5;

int n,m;
struct _ {
    int x,y,t;
};
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int s[20];
queue<_>q2[20];
char map[maxn][maxn];
int vis[maxn][maxn];

int expand(int p) {
    int newx = 0;
	queue<_> Q;
    while(!q2[p].empty())
    {
        _ x = q2[p].front();
        q2[p].pop();
        x.t = s[p];
        Q.push(x);
    }
    while(!Q.empty()) {
        _ x = Q.front();
        Q.pop();
        if (!x.t) {
            q2[p].push(x);
            continue;
        }
		REP(i,0,3) {
            int xx = x.x + dx[i];
            int yy = x.y + dy[i];
            if(xx<1||yy<1||xx>n||yy>m||map[xx][yy]=='#'||vis[xx][yy])continue;
            newx++;
            Q.push({xx,yy,x.t-1});  //步数+1
            vis[xx][yy] = p;   //记录棋盘
        }
    }
	return newx;
}
int main() {
	int p;
	scanf("%d%d%d", &n, &m, &p);
	REP(i,1,p) scanf("%d", s+i);
	REP(i,1,n) scanf("%s",map[i]+1);
    for(int i=1;i<=n;i++)
    {
        for (int j = 1; j <= m; j++)
        {
			if (isdigit(map[i][j])) {
                vis[i][j] = map[i][j] - '0';
                q2[vis[i][j]].push({i,j,s[vis[i][j]]});
            }
        }
    }

    while(true)
    {
        int flag = 0;
		REP(i,1,p) flag += expand(i);
		if (!flag) break;
    }
    int count[20];
    memset(count,0,sizeof count);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            count[vis[i][j]]++;
    for(int i=1;i<=p;i++)
        printf("%d ",count[i]);
	puts("");
}
/*
Input
3 2
1
1..
...
..2
Output
3
Input
4 4
1 1 1
....
#...
Output
4 3 3
 */


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head

const int N= 1e3+10;
int n, m, p;
struct _ {
	int x,y,v;
};
const int dx[]={1,-1,0,0};
const int dy[]={0,0,-1,1};
queue<_> q[N];
int a[N], vis[N][N], ans[N];
char s[N][N];

int expand(int id) {
	queue<_> Q;
	while (!q[id].empty()) {
		_ t = q[id].front();q[id].pop();
		t.v = a[id];
		Q.push(t);
	}
	int ok = 0;
	while (!Q.empty()) {
		_ t = Q.front();Q.pop();
		if (!t.v) {
			q[id].push(t);
			continue;
		}
		REP(i,0,3) {
			int x=t.x+dx[i],y=t.y+dy[i],v=t.v-1;
			if (x<=0||y<=0||x>n||y>m||vis[x][y]||s[x][y]=='#') continue;
			ok = 1;
			vis[x][y] = id;
			Q.push({x,y,v});
		}
	}
	return ok;
}

int main() {
	scanf("%d%d%d", &n, &m, &p);
	REP(i,1,p) scanf("%d", a+i);
	REP(i,1,n) scanf("%s", s[i]+1);
	REP(ii,1,p) REP(i,1,n) REP(j,1,m) {
		if (s[i][j]-'0'==ii) {
			vis[i][j]=ii;
			q[ii].push({i,j,a[i]});
		}
	}
	while (1) {
		int ok = 0;
		REP(i,1,p) ok |= expand(i);
		if (!ok) break;
	}
	REP(i,1,n) REP(j,1,m) ++ans[vis[i][j]];
	REP(i,1,p) printf("%d ",ans[i]);hr;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head

const int N= 1e3+10;
int n, m, p;
struct _ {
	int x,y,v;
};
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
queue<_> q[20];
int a[20], vis[N][N], ans[20];
char s[N][N];

int expand(int id) {
	queue<_> Q;
	while (!q[id].empty()) {
		_ t = q[id].front();q[id].pop();
		t.v = a[id];
		Q.push(t);
	}
	int ok = 0;
	while (!Q.empty()) {
		_ t = Q.front();Q.pop();
		if (!t.v) {
			q[id].push(t);
			continue;
		}
		REP(i,0,3) {
			int x=t.x+dx[i],y=t.y+dy[i],v=t.v-1;
			if (x<=0||y<=0||x>n||y>m||vis[x][y]||s[x][y]=='#') continue;
			ok = 1;
			vis[x][y] = id;
			Q.push({x,y,v});
		}
	}
	return ok;
}

int main() {
	scanf("%d%d%d", &n, &m, &p);
	REP(i,1,p) scanf("%d", a+i);
	REP(i,1,n) scanf("%s", s[i]+1);
	REP(i,1,n) REP(j,1,m) {
		if (isdigit(s[i][j])) {
			int ii = s[i][j]-'0';
			vis[i][j]=ii;
			q[ii].push({i,j,a[i]});
		}
	}
	while (1) {
		int ok = 0;
		REP(i,1,p) ok |= expand(i);
		if (!ok) break;
	}
	REP(i,1,n) REP(j,1,m) ++ans[vis[i][j]];
	REP(i,1,p) printf("%d ",ans[i]);hr;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head

const int N = 1e3+10;
int n, m, p;
int a[N], vis[N][N];
int ans[20];
char s[N][N];
const int dx[]={1,-1,0,-0};
const int dy[]={0,0,1,-1};
struct _ {
	int x,y,id,v;
};

int main() {
	scanf("%d%d%d", &n, &m, &p);
	REP(i,1,p) scanf("%d",a+i);
	REP(i,1,n) scanf("%s",s[i]+1);
	queue<_> q;
	REP(ii,1,p) REP(i,1,n) REP(j,1,m) {
		if (s[i][j]-'0'==ii) {
			q.push({i,j,ii,a[ii]});
			vis[i][j]=ii;
		}
	}
	while (!q.empty()) {
		int x=q.front().x,y=q.front().y,id=q.front().id;q.pop();
		queue<_> Q;
		REP(i,0,3) Q.push({x+dx[i],y+dy[i],id,a[id]-1});
		while (!Q.empty()) {
			int x=Q.front().x,y=Q.front().y,id=Q.front().id,v=Q.front().v;
			if (x<=0||x>n||y<=0||y>m||vis[x][y]||s[x][y]=='#') {
				Q.pop();
				continue;
			}
			q.push(Q.front());Q.pop();
			vis[x][y] = id;
			if (!v) continue;
			REP(i,0,3) Q.push({x+dx[i],y+dy[i],id,v-1});
		}
	}
	REP(i,1,n) REP(j,1,m) ++ans[vis[i][j]];
	REP(i,1,p) printf("%d ",ans[i]);
	puts("");
}