#include<bits/stdc++.h>
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define MOD 1000000007
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define FOR(a,n) for(int i=a;i<n;++i)
#define arrin(n) for(int i=0; i < n; ++i) cin>>arr[i];
#define arrout(n) for(int i=0; i < n; ++i) cout<<arr[i]<<" ";cout<<nl;
#define nl "\n"
#define mp make_pair
typedef unsigned long long ull;
typedef long long int ll;
typedef long double ld;
using namespace std;
const double pi=acos(-1.0);
const double pii=2*pi;
const double eps=1e-6;
const double inf=1e15;
typedef pair<ll,ll> iPair;
typedef priority_queue< iPair, vector <iPair> , greater<iPair>> Pq;
ll dp[200005];
ll dx[4]={+1,-1,0,0};
ll dy[4]={0,0,+1,-1};
ll n,m,c,p;
vector<iPair> r[10],rr[10];
bool check(ll x,ll y)
{
    if(x>=0 && x<n && y>=0 && y<m) return true;
    else return false;
}
//ll visited[1003][1003];
ll b[1002][1003];
string a[1003];
void BFS(queue<pair<ll,ll>> q,ll sp,ll t,ll f)
{
   while(!q.empty())
   {
      ll xx=q.front().first;
      ll yy=q.front().second;
      q.pop();
      if(b[xx][yy]==sp*(f+1)) break;
      for(ll i=0;i<4;i++)
      {
        if(check(xx+dx[i],yy+dy[i]) && a[xx+dx[i]][yy+dy[i]]=='.')
        {
             a[xx+dx[i]][yy+dy[i]]=t+48;
             c--;
             b[xx+dx[i]][yy+dy[i]]=b[xx][yy]+1;
             q.push(mp(xx+dx[i],yy+dy[i]));
             if(b[xx+dx[i]][yy+dy[i]]==sp*(f+1)) rr[t].pb(mp(xx+dx[i],yy+dy[i]));
        }
      }
   }
}
int main()
{
  fastio();
  cin>>n>>m>>p;
  ll s[p];
  for(ll i=0;i<p;i++) cin>>s[i];
  for(ll i=0;i<n;i++) cin>>a[i];
  c=0;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<m;j++)
    {
      if(a[i][j]=='.') c++;
      else if(a[i][j]-48<=9  && a[i][j]-48>0 ) rr[a[i][j]-48].pb(mp(i,j));
    }
  }
  ll t=1,f=0,z=0;
  while(c>0)
  {
        while(!r[t].empty()) r[t].pop_back(),z++;
        for(auto j: rr[t]) r[t].pb(j),z++;
        while(!rr[t].empty()) rr[t].pop_back(),z++;
        queue<pair<ll,ll>> q;
        for(auto i: r[t])
        {
            //cout<<i.first<<" "<<i.second<<endl;
            q.push(i);
            BFS(q,s[t-1],t,f);
        }
     t++;
     if(t==p+1) t=1,f++;
  }
  map<ll,ll> ans;
  for(ll i=0;i<n;i++)
     for(ll j=0;j<m;j++) ans[a[i][j]-48]++;
  for(ll i=1;i<=p;i++) cout<<ans[i]<<" ";
  cout<<nl;
  cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
  return 0;
}


#include<bits/stdc++.h>
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define MOD 1000000007
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define FOR(a,n) for(int i=a;i<n;++i)
#define arrin(n) for(int i=0; i < n; ++i) cin>>arr[i];
#define arrout(n) for(int i=0; i < n; ++i) cout<<arr[i]<<" ";cout<<nl;
#define nl "\n"
#define mp make_pair
typedef unsigned long long ull;
typedef long long int ll;
typedef long double ld;
using namespace std;
const double pi=acos(-1.0);
const double pii=2*pi;
const double eps=1e-6;
const double inf=1e15;
typedef pair<ll,ll> iPair;
typedef priority_queue< iPair, vector <iPair> , greater<iPair>> Pq;
ll dp[200005];
ll dx[4]={+1,-1,0,0};
ll dy[4]={0,0,+1,-1};
ll n,m,c;
vector<iPair> r[10],rr[10];
bool check(ll x,ll y)
{
    if(x>=0 && x<n && y>=0 && y<m) return true;
    else return false;
}
//ll visited[1003][1003];
ll b[1002][1003];
string a[1003];
void BFS(ll x,ll y,ll sp,ll t,ll f)
{
   queue<pair<ll,ll>> q;
   q.push(mp(x,y));
   while(!q.empty())
   {
      ll xx=q.front().first;
      ll yy=q.front().second;
      q.pop();
      if(b[xx][yy]==sp) break;
      for(ll i=0;i<4;i++)
      {
        if(check(xx+dx[i],yy+dy[i]) && a[xx+dx[i]][yy+dy[i]]=='.')
        {
             a[xx+dx[i]][yy+dy[i]]=t+48;
             c--;
             b[xx+dx[i]][yy+dy[i]]=b[xx][yy]+1;
             q.push(mp(xx+dx[i],yy+dy[i]));
             if(b[xx+dx[i]][yy+dy[i]]==sp) rr[t].pb(mp(xx+dx[i],yy+dy[i]));
        }
      }
   }
}
int main()
{
  fastio();
  //freopen("input.txt","r",stdin);
  //("output.txt","w",stdout);
  ll p;
  cin>>n>>m>>p;
  ll s[p];
  for(ll i=0;i<p;i++) cin>>s[i];
  for(ll i=0;i<n;i++)
  {
    cin>>a[i];
  }
  c=0;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<m;j++)
    {
      if(a[i][j]=='.') c++;
      else if(a[i][j]-48<=9  && a[i][j]-48>0 ) rr[a[i][j]-48].pb(mp(i,j));
    }
  }
  //cout<<c<<endl;
  ll t=1;
  ll f=0;
  ll z=0;
  while(c>0)
  {
          while(!r[t].empty()) r[t].pop_back(),z++;
          for(auto j: rr[t]) r[t].pb(j),z++;
          while(!rr[t].empty()) rr[t].pop_back(),z++;

        for(auto i: r[t])
        {
            //cout<<i.first<<" "<<i.second<<endl;
            if(b[i.first][i.second]==s[t-1]) b[i.first][i.second]=0;
            BFS(i.first,i.second,s[t-1],t,f);
        }
        /*
        //cout<<z<<endl;
     for(ll i=0;i<n;i++)
     {
       for(ll j=0;j<m;j++)
       {
           cout<<a[i][j]<<" ";
       }
       cout<<endl;
     }
     cout<<c<<nl;
     */
     t++;
     if(t==p+1) t=1,f++;
     //z++;
     //if(z==10) break;
  }
  //cout<<z<<nl;
  map<ll,ll> ans;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<m;j++)
    {
      ans[a[i][j]-48]++;
    }
  }

  //cout<<f<<nl;
  for(ll i=1;i<=p;i++) cout<<ans[i]<<" ";
  cout<<nl;
  //std::cout <<fixed<< std::setprecision(15) << ans << '\n';
  cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
  return 0;
}


#include<bits/stdc++.h>
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define MOD 1000000007
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define FOR(a,n) for(int i=a;i<n;++i)
#define arrin(n) for(int i=0; i < n; ++i) cin>>arr[i];
#define arrout(n) for(int i=0; i < n; ++i) cout<<arr[i]<<" ";cout<<nl;
#define nl "\n"
#define mp make_pair
typedef unsigned long long ull;
typedef long long int ll;
typedef long double ld;
using namespace std;
const double pi=acos(-1.0);
const double pii=2*pi;
const double eps=1e-6;
const double inf=1e15;
typedef pair<ll,ll> iPair;
typedef priority_queue< iPair, vector <iPair> , greater<iPair>> Pq;
ll dp[200005];
ll dx[4]={+1,-1,0,0};
ll dy[4]={0,0,+1,-1};
ll n,m,c;
vector<iPair> r[10][1000008];
bool check(ll x,ll y)
{
    if(x>=0 && x<n && y>=0 && y<m) return true;
    else return false;
}
ll visited[1003][1003];
ll b[1002][1003];
string a[1003];
void BFS(ll x,ll y,ll sp,ll t,ll f)
{
   queue<pair<ll,ll>> q;
   q.push(mp(x,y));
   while(!q.empty())
   {
      ll xx=q.front().first;
      ll yy=q.front().second;
      q.pop();
      if(b[xx][yy]==sp) break;
      visited[xx][yy]=1;
      for(ll i=0;i<4;i++)
      {
        if(check(xx+dx[i],yy+dy[i]) && a[xx+dx[i]][yy+dy[i]]=='.')
        {
             a[xx+dx[i]][yy+dy[i]]=t+48;
             c--;
             b[xx+dx[i]][yy+dy[i]]=b[xx][yy]+1;
             q.push(mp(xx+dx[i],yy+dy[i]));
             if(b[xx+dx[i]][yy+dy[i]]==sp) r[t][f+1].pb(mp(xx+dx[i],yy+dy[i]));
        }
      }
   }
}
int main()
{
  fastio();
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  ll p;
  cin>>n>>m>>p;
  ll s[p];
  for(ll i=0;i<p;i++) cin>>s[i];
  for(ll i=0;i<n;i++)
  {
    cin>>a[i];
  }
  c=0;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<m;j++)
    {
      if(a[i][j]=='.') c++;
      else if(a[i][j]-48<=9  && a[i][j]-48>0 ) r[a[i][j]-48][0].pb(mp(i,j));
    }
  }
  //cout<<c<<endl;
  ll t=1;
  ll f=0;
  ll z=0;
  while(c>0)
  {
        for(auto i: r[t][f])
        {
            //cout<<i.first<<" "<<i.second<<endl;
            visited[i.first][i.second]=1;
            if(b[i.first][i.second]==s[t-1]) b[i.first][i.second]=0;
            BFS(i.first,i.second,s[t-1],t,f);
            z++;
        }
        //cout<<z<<endl;
        /*
     for(ll i=0;i<n;i++)
     {
       for(ll j=0;j<m;j++)
       {
           cout<<a[i][j]<<" ";
       }
       cout<<endl;
     }
     cout<<c<<nl;
     */
     t++;
     if(t==p+1) t=1,f++;
  }
  map<ll,ll> ans;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<m;j++)
    {
      ans[a[i][j]-48]++;
    }
  }
  for(ll i=1;i<=p;i++) cout<<ans[i]<<" ";
  cout<<nl;
  //std::cout <<fixed<< std::setprecision(15) << ans << '\n';
  cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
  return 0;
}


#include<bits/stdc++.h>
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define MOD 1000000007
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define FOR(a,n) for(int i=a;i<n;++i)
#define arrin(n) for(int i=0; i < n; ++i) cin>>arr[i];
#define arrout(n) for(int i=0; i < n; ++i) cout<<arr[i]<<" ";cout<<nl;
#define nl "\n"
#define mp make_pair
typedef unsigned long long ull;
typedef long long int ll;
typedef long double ld;
using namespace std;
const double pi=acos(-1.0);
const double pii=2*pi;
const double eps=1e-6;
const double inf=1e15;
typedef pair<ll,ll> iPair;
typedef priority_queue< iPair, vector <iPair> , greater<iPair>> Pq;
ll dp[200005];
ll dx[4]={+1,-1,0,0};
ll dy[4]={0,0,+1,-1};
ll n,m,c,p;
vector<iPair> r[10],rr[10];
bool check(ll x,ll y)
{
    if(x>=0 && x<n && y>=0 && y<m) return true;
    else return false;
}
//ll visited[1003][1003];
ll b[1002][1003];
string a[1003];
void BFS(queue<pair<ll,ll>> q,ll sp,ll t,ll f)
{
   while(!q.empty())
   {
      ll xx=q.front().first;
      ll yy=q.front().second;
      q.pop();
      if(b[xx][yy]==sp) break;
      for(ll i=0;i<4;i++)
      {
        if(check(xx+dx[i],yy+dy[i]) && a[xx+dx[i]][yy+dy[i]]=='.')
        {
             a[xx+dx[i]][yy+dy[i]]=t+48;
             c--;
             b[xx+dx[i]][yy+dy[i]]=b[xx][yy]+1;
             q.push(mp(xx+dx[i],yy+dy[i]));
             if(b[xx+dx[i]][yy+dy[i]]==sp) rr[t].pb(mp(xx+dx[i],yy+dy[i]));
        }
      }
   }
}
int main()
{
  fastio();
  cin>>n>>m>>p;
  ll s[p];
  for(ll i=0;i<p;i++) cin>>s[i];
  for(ll i=0;i<n;i++) cin>>a[i];
  c=0;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<m;j++)
    {
      if(a[i][j]=='.') c++;
      else if(a[i][j]-48<=9  && a[i][j]-48>0 ) rr[a[i][j]-48].pb(mp(i,j));
    }
  }
  ll t=1,f=0,z=0;
  while(c>0)
  {
        while(!r[t].empty()) r[t].pop_back(),z++;
        for(auto j: rr[t]) r[t].pb(j),z++;
        while(!rr[t].empty()) rr[t].pop_back(),z++;
        queue<pair<ll,ll>> q;
        for(auto i: r[t])
        {
            //cout<<i.first<<" "<<i.second<<endl;
            b[i.first][i.second]=0;
            q.push(i);
            BFS(q,s[t-1],t,f);
        }
     t++;
     if(t==p+1) t=1,f++;
  }
  map<ll,ll> ans;
  for(ll i=0;i<n;i++)
     for(ll j=0;j<m;j++) ans[a[i][j]-48]++;
  for(ll i=1;i<=p;i++) cout<<ans[i]<<" ";
  cout<<nl;
  cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
  return 0;
}


#include<bits/stdc++.h>
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define MOD 1000000007
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define FOR(a,n) for(int i=a;i<n;++i)
#define arrin(n) for(int i=0; i < n; ++i) cin>>arr[i];
#define arrout(n) for(int i=0; i < n; ++i) cout<<arr[i]<<" ";cout<<nl;
#define nl "\n"
#define mp make_pair
typedef unsigned long long ull;
typedef long long int ll;
typedef long double ld;
using namespace std;
const double pi=acos(-1.0);
const double pii=2*pi;
const double eps=1e-6;
const double inf=1e15;
typedef pair<ll,ll> iPair;
typedef priority_queue< iPair, vector <iPair> , greater<iPair>> Pq;
ll dp[200005];
ll dx[4]={+1,-1,0,0};
ll dy[4]={0,0,+1,-1};
ll n,m,c;
vector<iPair> r[9][10000];
bool check(ll x,ll y)
{
    if(x>=0 && x<n && y>=0 && y<m) return true;
    else return false;
}
ll visited[1003][1003];
ll b[1002][1003];
string a[1003];
void BFS(ll x,ll y,ll sp,ll t,ll f)
{
   queue<pair<ll,ll>> q;
   q.push(mp(x,y));
   while(!q.empty())
   {
      ll xx=q.front().first;
      ll yy=q.front().second;
      q.pop();
      if(b[xx][yy]==sp) break;
      visited[xx][yy]=1;
      for(ll i=0;i<4;i++)
      {
        if(check(xx+dx[i],yy+dy[i]) && a[xx+dx[i]][yy+dy[i]]=='.')
        {
             a[xx+dx[i]][yy+dy[i]]=t+48;
             c--;
             b[xx+dx[i]][yy+dy[i]]=b[xx][yy]+1;
             q.push(mp(xx+dx[i],yy+dy[i]));
             if(b[xx+dx[i]][yy+dy[i]]==sp) r[t][f+1].pb(mp(xx+dx[i],yy+dy[i]));
        }
      }
   }
}
int main()
{
  fastio();
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  ll p;
  cin>>n>>m>>p;
  ll s[p];
  for(ll i=0;i<p;i++) cin>>s[i];
  for(ll i=0;i<n;i++)
  {
    cin>>a[i];
  }
  c=0;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<m;j++)
    {
      if(a[i][j]=='.') c++;
      else if(a[i][j]-48<=9  && a[i][j]-48>0 ) r[a[i][j]-48][0].pb(mp(i,j));
    }
  }
  //cout<<c<<endl;
  ll t=1;
  ll f=0;
  ll z=0;
  while(c>0)
  {
        for(auto i: r[t][f])
        {
            //cout<<i.first<<" "<<i.second<<endl;
            visited[i.first][i.second]=1;
            if(b[i.first][i.second]==s[t-1]) b[i.first][i.second]=0;
            BFS(i.first,i.second,s[t-1],t,f);
            z++;
        }
        //cout<<z<<endl;
        /*
     for(ll i=0;i<n;i++)
     {
       for(ll j=0;j<m;j++)
       {
           cout<<a[i][j]<<" ";
       }
       cout<<endl;
     }
     cout<<c<<nl;
     */
     t++;
     if(t==p+1) t=1,f++;
  }
  map<ll,ll> ans;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<m;j++)
    {
      ans[a[i][j]-48]++;
    }
  }
  for(ll i=1;i<=p;i++) cout<<ans[i]<<" ";
  cout<<nl;
  //std::cout <<fixed<< std::setprecision(15) << ans << '\n';
  cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
  return 0;
}


#include<bits/stdc++.h>
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define MOD 1000000007
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define FOR(a,n) for(int i=a;i<n;++i)
#define arrin(n) for(int i=0; i < n; ++i) cin>>arr[i];
#define arrout(n) for(int i=0; i < n; ++i) cout<<arr[i]<<" ";cout<<nl;
#define nl "\n"
#define mp make_pair
typedef unsigned long long ull;
typedef long long int ll;
typedef long double ld;
using namespace std;
const double pi=acos(-1.0);
const double pii=2*pi;
const double eps=1e-6;
const double inf=1e15;
typedef pair<ll,ll> iPair;
typedef priority_queue< iPair, vector <iPair> , greater<iPair>> Pq;
ll dp[200005];
ll dx[4]={+1,-1,0,0};
ll dy[4]={0,0,+1,-1};
ll n,m,c,p;
vector<iPair> r[10],rr[10];
bool check(ll x,ll y)
{
    if(x>=0 && x<n && y>=0 && y<m) return true;
    else return false;
}
//ll visited[1003][1003];
ll b[1002][1003];
string a[1003];
void BFS(queue<pair<ll,ll>> q,ll sp,ll t,ll f)
{
   while(!q.empty())
   {
      ll xx=q.front().first;
      ll yy=q.front().second;
      q.pop();
      if(b[xx][yy]==sp*(f+1)) break;
      for(ll i=0;i<4;i++)
      {
        if(check(xx+dx[i],yy+dy[i]) && a[xx+dx[i]][yy+dy[i]]=='.')
        {
             a[xx+dx[i]][yy+dy[i]]=t+48;
             c--;
             b[xx+dx[i]][yy+dy[i]]=b[xx][yy]+1;
             q.push(mp(xx+dx[i],yy+dy[i]));
             if(b[xx+dx[i]][yy+dy[i]]==sp*(f+1)) rr[t].pb(mp(xx+dx[i],yy+dy[i]));
        }
      }
   }
}
int main()
{
  fastio();
  cin>>n>>m>>p;
  ll s[p];
  for(ll i=0;i<p;i++) cin>>s[i];
  for(ll i=0;i<n;i++) cin>>a[i];
  c=0;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<m;j++)
    {
      if(a[i][j]=='.') c++;
      else if(a[i][j]-48<=9  && a[i][j]-48>0 ) rr[a[i][j]-48].pb(mp(i,j));
    }
  }
  ll t=1,f=0,z=0;
  ll e=0;
  while(1)
  {
        while(!r[t].empty()) r[t].pop_back(),z++;
        for(auto j: rr[t]) r[t].pb(j),z++;
        while(!rr[t].empty()) rr[t].pop_back(),z++;
        queue<pair<ll,ll>> q;
        for(auto i: r[t]) q.push(i);
        BFS(q,s[t-1],t,f);
        if(q.size()==0) e++;
        else e=0;
        //cout<<e<<endl;
        if(e==p) break;
     t++;
     if(t==p+1) t=1,f++;
  }
  map<ll,ll> ans;
  for(ll i=0;i<n;i++)
     for(ll j=0;j<m;j++) ans[a[i][j]-48]++;
  for(ll i=1;i<=p;i++) cout<<ans[i]<<" ";
  cout<<nl;
  cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
  return 0;
}


#include<bits/stdc++.h>
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define MOD 1000000007
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define FOR(a,n) for(int i=a;i<n;++i)
#define arrin(n) for(int i=0; i < n; ++i) cin>>arr[i];
#define arrout(n) for(int i=0; i < n; ++i) cout<<arr[i]<<" ";cout<<nl;
#define nl "\n"
#define mp make_pair
typedef unsigned long long ull;
typedef long long int ll;
typedef long double ld;
using namespace std;
const double pi=acos(-1.0);
const double pii=2*pi;
const double eps=1e-6;
//const double inf=1e15;
typedef pair<ll,ll> iPair;
typedef priority_queue< iPair, vector <iPair> , greater<iPair>> Pq;
ll dp[200005];
ll dx[4]={+1,-1,0,0};
ll dy[4]={0,0,+1,-1};
ll n,m,c,p;
vector<iPair> r[10],rr[10];
bool check(ll x,ll y)
{
    if(x>=0 && x<n && y>=0 && y<m) return true;
    else return false;
}
//ll visited[1003][1003];
ll b[1002][1003];
string a[1003];
void BFS(queue<pair<ll,ll>> q,ll sp,ll t,ll f)
{
   while(!q.empty())
   {
      ll xx=q.front().first;
      ll yy=q.front().second;
      q.pop();
      if(b[xx][yy]==sp*(f+1)) break;
      for(ll i=0;i<4;i++)
      {
        if(check(xx+dx[i],yy+dy[i]) && a[xx+dx[i]][yy+dy[i]]=='.')
        {
             a[xx+dx[i]][yy+dy[i]]=t+48;
             c--;
             b[xx+dx[i]][yy+dy[i]]=b[xx][yy]+1;
             q.push(mp(xx+dx[i],yy+dy[i]));
             if(b[xx+dx[i]][yy+dy[i]]==sp*(f+1)) rr[t].pb(mp(xx+dx[i],yy+dy[i]));
        }
      }
   }
}
int main()
{
  fastio();
  cin>>n>>m>>p;
  ll s[p];
  for(ll i=0;i<p;i++) cin>>s[i];
  for(ll i=0;i<n;i++) cin>>a[i];
  c=0;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<m;j++)
    {
      if(a[i][j]=='.') c++;
      else if(a[i][j]-48<=9  && a[i][j]-48>0 ) rr[a[i][j]-48].pb(mp(i,j));
    }
  }
  ll t=1,f=0,z=0;
  ll e=0;
  while(1)
  {
        while(!r[t].empty()) r[t].pop_back(),z++;
        for(auto j: rr[t]) r[t].pb(j),z++;
        while(!rr[t].empty()) rr[t].pop_back(),z++;
        queue<pair<ll,ll>> q;
        for(auto i: r[t]) q.push(i);
        BFS(q,s[t-1],t,f);
        if(q.size()==0) e++;
        else e=0;
        //cout<<e<<endl;
        if(e==p) break;
     t++;
     if(t==p+1) t=1,f++;
  }
  map<ll,ll> ans;
  for(ll i=0;i<n;i++)
     for(ll j=0;j<m;j++) ans[a[i][j]-48]++;
  for(ll i=1;i<=p;i++) cout<<ans[i]<<" ";
  cout<<nl;
  cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
  return 0;
}


#include<bits/stdc++.h>
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define MOD 1000000007
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define FOR(a,n) for(int i=a;i<n;++i)
#define arrin(n) for(int i=0; i < n; ++i) cin>>arr[i];
#define arrout(n) for(int i=0; i < n; ++i) cout<<arr[i]<<" ";cout<<nl;
#define nl "\n"
#define mp make_pair
typedef unsigned long long ull;
typedef long long int ll;
typedef long double ld;
using namespace std;
const double pi=acos(-1.0);
const double pii=2*pi;
const double eps=1e-6;
const double inf=1e15;
typedef pair<ll,ll> iPair;
typedef priority_queue< iPair, vector <iPair> , greater<iPair>> Pq;
ll dp[200005];
ll dx[4]={+1,-1,0,0};
ll dy[4]={0,0,+1,-1};
ll n,m,c;
vector<iPair> r[10],rr[10];
bool check(ll x,ll y)
{
    if(x>=0 && x<n && y>=0 && y<m) return true;
    else return false;
}
ll visited[1003][1003];
ll b[1002][1003];
string a[1003];
void BFS(ll x,ll y,ll sp,ll t,ll f)
{
   queue<pair<ll,ll>> q;
   q.push(mp(x,y));
   while(!q.empty())
   {
      ll xx=q.front().first;
      ll yy=q.front().second;
      q.pop();
      if(b[xx][yy]==sp) break;
      visited[xx][yy]=1;
      for(ll i=0;i<4;i++)
      {
        if(check(xx+dx[i],yy+dy[i]) && a[xx+dx[i]][yy+dy[i]]=='.')
        {
             a[xx+dx[i]][yy+dy[i]]=t+48;
             c--;
             b[xx+dx[i]][yy+dy[i]]=b[xx][yy]+1;
             q.push(mp(xx+dx[i],yy+dy[i]));
             if(b[xx+dx[i]][yy+dy[i]]==sp) rr[t].pb(mp(xx+dx[i],yy+dy[i]));
        }
      }
   }
}
int main()
{
  fastio();
  //freopen("input.txt","r",stdin);
  //("output.txt","w",stdout);
  ll p;
  cin>>n>>m>>p;
  ll s[p];
  for(ll i=0;i<p;i++) cin>>s[i];
  for(ll i=0;i<n;i++)
  {
    cin>>a[i];
  }
  c=0;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<m;j++)
    {
      if(a[i][j]=='.') c++;
      else if(a[i][j]-48<=9  && a[i][j]-48>0 ) rr[a[i][j]-48].pb(mp(i,j));
    }
  }
  //cout<<c<<endl;
  ll t=1;
  ll f=0;
  ll z=0;
  while(c>0)
  {
          while(!r[t].empty()) r[t].pop_back();
          for(auto j: rr[t]) r[t].pb(j);
          while(!rr[t].empty()) rr[t].pop_back();

        for(auto i: r[t])
        {
            //cout<<i.first<<" "<<i.second<<endl;
            visited[i.first][i.second]=1;
            if(b[i.first][i.second]==s[t-1]) b[i.first][i.second]=0;
            BFS(i.first,i.second,s[t-1],t,f);
        }
        //cout<<z<<endl;
        /*
     for(ll i=0;i<n;i++)
     {
       for(ll j=0;j<m;j++)
       {
           cout<<a[i][j]<<" ";
       }
       cout<<endl;
     }
     cout<<c<<nl;
     */
     t++;
     if(t==p+1) t=1,f++;
     //z++;
     //if(z==10) break;
  }
  map<ll,ll> ans;
  for(ll i=0;i<n;i++)
  {
    for(ll j=0;j<m;j++)
    {
      ans[a[i][j]-48]++;
    }
  }
  //cout<<f<<nl;
  for(ll i=1;i<=p;i++) cout<<ans[i]<<" ";
  cout<<nl;
  //std::cout <<fixed<< std::setprecision(15) << ans << '\n';
  cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
  return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[] = {1, -1, 0, 0}, dk[] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11];
    bool emp;
    vector<pii> pos[11];
    cin >> n >> m >> p;
    for(int i = 1; i <= p; i++) cin >> speed[i];
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            cin >> g[i][j];
            d[i][j] = INT_MAX;
            if(g[i][j] != '.' && g[i][j] != '#') pos[g[i][j] - '0'].pb({i, j}), cont[g[i][j] - '0']++;
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }
        if(emp) break;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            for(auto p : pos[i]){
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    //d[j][k] = 0;
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
    }
    for(int i = 1; i <= p; i++) cout << cont[i] << " ";
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0};
    bool emp;
    vector<pii> pos[11];
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].pb({i, j});
                cont[g[i][j] - '0']++;
            }
        }
    }
    while(1){
        emp = true;
        /*for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }*/
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            if(pos[i].empty()) continue;
            for(auto p : pos[i]){
                emp = false;
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
        if(emp) break;
    }
    printf("%d", cont[1]);
    for(int i = 2; i <= p; i++) printf(" %d", cont[i]);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0};
    bool emp;
    vector<pii> pos[11];
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].pb({i, j});
                cont[g[i][j] - '0']++;
            }
        }
    }
    while(1){
        emp = true;
        /*for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }*/
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            //if(pos[i].empty()) continue;
            for(auto p : pos[i]){
                emp = false;
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
        if(emp) break;
    }
    printf("%d", cont[1]);
    for(int i = 2; i <= p; i++) printf(" %d", cont[i]);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0};
    bool emp;
    vector<pii> pos[11];
    scanf("%d%d%d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].pb({i, j});
                cont[g[i][j] - '0']++;
            }
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }
        if(emp) break;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            if(pos[i].empty()) continue;
            for(auto p : pos[i]){
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    //d[j][k] = 0;
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.'){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
    }
    cout << cont[1];
    for(int i = 2; i <= p; i++) cout << " " << cont[i];
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11];
    bool emp;
    vector<pii> pos[11];
    cin >> n >> m >> p;
    for(int i = 1; i <= p; i++) cin >> speed[i];
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            cin >> g[i][j];
            d[i][j] = INT_MAX;
            if(g[i][j] != '.' && g[i][j] != '#') pos[g[i][j] - '0'].pb({i, j}), cont[g[i][j] - '0']++;
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }
        if(emp) break;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            for(auto p : pos[i]){
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    d[j][k] = 0;
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
    }
    cout << cont[1];
    for(int i = 2; i <= p; i++) cout << " " << cont[i];
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11];
    bool emp;
    vector<pii> pos[11];
    scanf("%d%d%d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].pb({i, j});
                cont[g[i][j] - '0']++;
            }
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }
        if(emp) break;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            for(auto p : pos[i]){
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    d[j][k] = 0;
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
    }
    cout << cont[1];
    for(int i = 2; i <= p; i++) cout << " " << cont[i];
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0};
    bool emp;
    vector<pii> pos[11];
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        //getchar();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].pb({i, j});
                cont[g[i][j] - '0']++;
            }
        }
    }
    while(1){
        emp = true;
        /*for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }*/
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            //if(pos[i].empty()) continue;
            for(auto p : pos[i]){
                emp = false;
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
        if(emp) break;
    }
    printf("%d", cont[1]);
    for(int i = 2; i <= p; i++) printf(" %d", cont[i]);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0};
    bool emp;
    vector<pii> pos[11];
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar_unlocked();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].pb({i, j});
                cont[g[i][j] - '0']++;
            }
        }
    }
    while(1){
        emp = true;
        /*for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }*/
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            //if(pos[i].empty()) continue;
            for(auto p : pos[i]){
                emp = false;
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
        if(emp) break;
    }
    printf("%d", cont[1]);
    for(int i = 2; i <= p; i++) printf(" %d", cont[i]);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[] = {1, -1, 0, 0}, dk[] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11];
    bool emp;
    vector<pii> pos[11];
    cin >> n >> m >> p;
    for(int i = 1; i <= p; i++) cin >> speed[i];
    for(int i = 0; i < n; i++){
        getchar();
        cin >> g[i];
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            d[i][j] = INT_MAX;
            if(g[i][j] != '.' && g[i][j] != '#') pos[g[i][j] - '0'].pb({i, j}), cont[g[i][j] - '0']++;
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }
        if(emp) break;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            for(auto p : pos[i]){
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    d[j][k] = 0;
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
    }
    for(int i = 1; i <= p; i++) cout << cont[i] << " ";
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11];
    bool emp;
    vector<pii> pos[11];
    cin >> n >> m >> p;
    for(int i = 1; i <= p; i++) cin >> speed[i];
    for(int i = 0; i < n; i++){
        //getchar();
        for(int j = 0; j < m; j++){
            cin >> g[i][j];
            d[i][j] = INT_MAX;
            if(g[i][j] != '.' && g[i][j] != '#') pos[g[i][j] - '0'].pb({i, j}), cont[g[i][j] - '0']++;
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }
        if(emp) break;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            for(auto p : pos[i]){
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    d[j][k] = 0;
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
    }
    cout << cont[1];
    for(int i = 2; i <= p; i++) cout << " " << cont[i];
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
struct node{
    int i, j, t;
};
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0}, turn = 0;
    bool emp;
    queue<node> pos[11];
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].push({i, j, turn});
                cont[g[i][j] - '0']++;
                d[i][j] = 0;
            }
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            while(!pos[i].empty() && pos[i].front().t == turn){
                int j = pos[i].front().i, k = pos[i].front().j;
                pos[i].pop();
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        emp = false;
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        if(d[nj][nk] < speed[i]) pos[i].push({nj, nk, turn});
                        else{
                            pos[i].push({nj, nk, turn + 1});
                            d[nj][nk] = 0;
                        }
                    }
                }
            }
        }
        if(emp) break;
        turn++;
    }
    for(int i = 1; i <= p; i++) printf("%d ", cont[i]);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0};
    bool emp;
    vector<pii> pos[11];
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar()_unlocked();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].pb({i, j});
                cont[g[i][j] - '0']++;
            }
        }
    }
    while(1){
        emp = true;
        /*for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }*/
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            //if(pos[i].empty()) continue;
            for(auto p : pos[i]){
                emp = false;
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
        if(emp) break;
    }
    printf("%d", cont[1]);
    for(int i = 2; i <= p; i++) printf(" %d", cont[i]);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0};
    bool emp;
    vector<pii> pos[11];
    scanf("%d%d%d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].pb({i, j});
                cont[g[i][j] - '0']++;
            }
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }
        if(emp) break;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            if(pos[i].empty()) continue;
            for(auto p : pos[i]){
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    //d[j][k] = 0;
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
    }
    printf("%d", cont[1]);
    for(int i = 2; i <= p; i++) printf(" %d", cont[i]);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0};
    bool emp;
    vector<pii> pos[11];
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].pb({i, j});
                cont[g[i][j] - '0']++;
            }
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            if(pos[i].empty()) continue;
            for(auto p : pos[i]){
                emp = false;
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        if(d[nj][nk] < speed[i]) q.push({nj, nk});
                        else pos[i].pb({nj, nk});
                    }
                }
            }
        }
        if(emp) break;
    }
    printf("%d", cont[1]);
    for(int i = 2; i <= p; i++) printf(" %d", cont[i]);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[] = {1, -1, 0, 0}, dk[] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11];
    bool emp;
    vector<pii> pos[11];
    cin >> n >> m >> p;
    for(int i = 1; i <= p; i++) cin >> speed[i];
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            cin >> g[i][j];
            d[i][j] = INT_MAX;
            if(g[i][j] != '.' && g[i][j] != '#') pos[g[i][j] - '0'].pb({i, j}), cont[g[i][j] - '0']++;
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }
        if(emp) break;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            for(auto p : pos[i]){
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    d[j][k] = 0;
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
    }
    for(int i = 1; i <= p; i++) cout << cont[i] << " ";
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0};
    bool emp;
    vector<pii> pos[11];
    scanf("%d%d%d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].pb({i, j});
                cont[g[i][j] - '0']++;
            }
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }
        if(emp) break;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            for(auto p : pos[i]){
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    d[j][k] = 0;
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
    }
    cout << cont[1];
    for(int i = 2; i <= p; i++) cout << " " << cont[i];
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[] = {1, -1, 0, 0}, dk[] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11];
    bool emp;
    vector<pii> pos[11];
    //scanf("%d %d %d", &n, &m, &p);
    cin >> n >> m >> p;
    for(int i = 1; i <= p; i++) cin >> speed[i];//scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        cin >> g[i];//scanf("%s", g[i]);

    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            d[i][j] = INT_MAX;
            if(g[i][j] != '.' && g[i][j] != '#') pos[g[i][j] - '0'].pb({i, j}), cont[g[i][j] - '0']++;
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }
        if(emp) break;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            for(auto p : pos[i]){
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    d[j][k] = 0;
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
    }
    for(int i = 1; i <= p; i++) cout << cont[i] << " ";//printf("%d ", cont[i]);
    return 0;
}//y


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
struct node{
    int i, j, t;
};
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0}, turn = 0;
    bool emp;
    //vector<pii> pos[11];
    queue<node> pos[11];
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                //pos[g[i][j] - '0'].pb({i, j});
                pos[g[i][j] - '0'].push({i, j, turn});
                cont[g[i][j] - '0']++;
                d[i][j] = 0;
            }
        }
    }
    while(1){
        emp = true;
        /*for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }*/
        for(int i = 1; i <= p; i++){
            /*queue<pii> q;
            if(pos[i].empty()) continue;
            for(auto p : pos[i]){
                emp = false;
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();*/
            while(!pos[i].empty() && pos[i].front().t == turn){
                int j = pos[i].front().i, k = pos[i].front().j;
                pos[i].pop();
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        emp = false;
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        if(d[nj][nk] < speed[i]) pos[i].push({nj, nk, turn});
                        else{
                            pos[i].push({nj, nk, turn + 1});
                            d[nj][nk] = 0;
                        }
                    }
                }
            }
        }
        if(emp) break;
        turn++;
    }
    printf("%d", cont[1]);
    for(int i = 2; i <= p; i++) printf(" %d", cont[i]);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0};
    bool emp;
    vector<pii> pos[11];
    scanf("%d%d%d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].pb({i, j});
                cont[g[i][j] - '0']++;
            }
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }
        if(emp) break;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            if(pos[i].empty()) continue;
            for(auto p : pos[i]){
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    //d[j][k] = 0;
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
    }
    cout << cont[1];
    for(int i = 2; i <= p; i++) cout << " " << cont[i];
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fp first
#define sp second
#define mp make_pair
#define ins insert
#define uEdge(u, v) g[u].pb(v), g[v].pb(u)
#define uwEdge(u, v, w) g[u].pb({v, w}), g[v].pb({u, w})
#define dEdge(u, v) g[u].pb(v)
#define dwEdge(u, v, w) g[u].pb({v, w})

using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
char g[1100][1100];
int d[1100][1100], dj[4] = {1, -1, 0, 0}, dk[4] = {0, 0, 1, -1};

int main(){
    int n, m, p, speed[11], cont[11]={0};
    bool emp;
    vector<pii> pos[11];
    scanf("%d%d%d", &n, &m, &p);
    for(int i = 1; i <= p; i++) scanf("%d", &speed[i]);
    for(int i = 0; i < n; i++){
        getchar();
        for(int j = 0; j < m; j++){
            scanf("%c", &g[i][j]);
            d[i][j] = INT_MAX;
            if('1' <= g[i][j] && g[i][j] <= '9'){
                pos[g[i][j] - '0'].pb({i, j});
                cont[g[i][j] - '0']++;
            }
        }
    }
    while(1){
        emp = true;
        for(int i = 1; i <= p; i++){
            if(!pos[i].empty()){
                emp = false;
                break;
            }
        }
        if(emp) break;
        for(int i = 1; i <= p; i++){
            queue<pii> q;
            if(pos[i].empty()) continue;
            for(auto p : pos[i]){
                q.push(p);
                d[p.fp][p.sp] = 0;
            }
            pos[i].clear();
            while(!q.empty()){
                int j = q.front().fp, k = q.front().sp;
                q.pop();
                if(d[j][k] == speed[i]){
                    pos[i].pb({j, k});
                    d[j][k] = 0;
                    continue;
                }
                for(int l = 0; l < 4; l++){
                    int nj = j + dj[l], nk = k + dk[l];
                    if(nj < 0 || nj >= n || nk < 0 || nk >= m) continue;
                    if(g[nj][nk] == '.' && d[j][k] + 1 < d[nj][nk]){
                        d[nj][nk] = d[j][k] + 1;
                        g[nj][nk] = i + '0';
                        cont[i]++;
                        q.push({nj, nk});
                    }
                }
            }
        }
    }
    cout << cont[1];
    for(int i = 2; i <= p; i++) cout << " " << cont[i];
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod = 1000000007;
int powe(int x, int y){ x = x%mod;int ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
int n,m,p;
string arr[1005];
int sp[10];
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int main(){
    fast;
    cin>>n>>m>>p;
    for (int i = 0; i<p; i++){
    	cin>>sp[i];
    	sp[i] = min(sp[i],10000);
    }
    for (int i = 0; i<n; i++)
    	cin>>arr[i];
    vector< pair<int,int> >cor[10],empty,temp;
    int ctr[10]={};
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		if (arr[i][j]== '.' || arr[i][j] == '#')
    			continue;
    		cor[arr[i][j]-'0'-1].PB({i,j});
    		ctr[arr[i][j]-'0'-1]++;
    	}
    }
    bool move = true;
    while(move){
    	move = false;
    	for (int i = 0; i<p; i++){
    		for (int l = 0; l<sp[i]; l++){
    		for (int a = cor[i].size()-1; a>=0; a--){
    			int x=cor[i][a].F, y = cor[i][a].S;
    			for (int k = 0; k<4; k++){
    				if (x+dx[k] < n && x+dx[k]>=0 && y+dy[k]>=0 && y+dy[k]<m){
    					if (arr[x+dx[k]][y+dy[k]] == '.'){
    						arr[x+dx[k]][y+dy[k]] = '1'+i;
    						temp.PB({x+dx[k],y+dy[k]});
    						ctr[i]++;
    						move = true;
    					}
    				}
    			}
    		}
    		swap(cor[i],temp);
    		}
    	temp = empty;
    	}
    }
    for (int i = 0; i<p; i++){
    	cout<<ctr[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod = 1000000007;
int powe(int x, int y){ x = x%mod;int ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
int n,m,p;
string arr[1005];
int sp[10];
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int main(){
    fast;
    cin>>n>>m>>p;
    for (int i = 0; i<p; i++){
    	cin>>sp[i];
    	sp[i] = min(sp[i],2005);
    }
    for (int i = 0; i<n; i++)
    	cin>>arr[i];
    vector< pair<int,int> >cor[10],empty,temp;
    int ctr[10]={};
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		if (arr[i][j]== '.' || arr[i][j] == '#')
    			continue;
    		cor[arr[i][j]-'0'-1].PB({i,j});
    		ctr[arr[i][j]-'0'-1]++;
    	}
    }
    bool move = true;
    while(move){
    	move = false;
    	for (int i = 0; i<p; i++){
    		for (int l = 0; l<sp[i] && cor[i].size(); l++){
    		for (int a = cor[i].size()-1; a>=0; a--){
    			int x=cor[i][a].F, y = cor[i][a].S;
    			for (int k = 0; k<4; k++){
    				if (x+dx[k] < n && x+dx[k]>=0 && y+dy[k]>=0 && y+dy[k]<m){
    					if (arr[x+dx[k]][y+dy[k]] == '.'){
    						arr[x+dx[k]][y+dy[k]] = '1'+i;
    						temp.PB({x+dx[k],y+dy[k]});
    						ctr[i]++;
    						move = true;
    					}
    				}
    			}
    		}
    		cor[i]=temp;
    		temp = empty;
    		}
    	}
    }
    for (int i = 0; i<p; i++){
    	cout<<ctr[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod = 1000000007;
int powe(int x, int y){ x = x%mod;int ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
int inp[1003][1003];
int now[1003][1003];
int proc[1003][1003];
int sp[11];
int n,m,p;
bool fin;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
vector< pair<int,int> > pos[11];
void recur(int x, int y, int moves, int p){
	if (x >=n || x<0 || y>=m || y<0 || moves<0)
		return;
	if (proc[x][y]||(inp[x][y]>0 && inp[x][y]!=p) || (inp[x][y] == -1) || (inp[x][y] == p &&now[x][y]>=moves ))
		return;
	inp[x][y] = p;
	now[x][y] = moves;
	pos[p].PB(MP(x,y));
	for (int j = 0; j<4; j++)
		recur(x+dx[j],y+dy[j],moves-1,p);
	fin=false;
}
int main(){
    fast;
    cin>>n>>m>>p;
    for (int i=1; i<=p; i++){
    	cin>>sp[i];
    }
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		char a;
    		cin>>a;
    		if (a == '#')
    			inp[i][j] = -1;
    		else if (a == '.')
    			inp[i][j] = 0;
    		else{
    			inp[i][j] = a-'0';
    			pos[a-'0'].PB(MP(i,j));
    		}
    	}
    }
    fin = false;
    while(!fin){
    	fin = true;
    	for (int i = 1; i<=p; i++){
    		for (auto x:pos[i]){
    				for (int j = 0; j<4; j++){
    					now[x.F][x.S]=sp[i];
    					proc[x.F][x.S]=1;
    					recur(x.F+dx[j], x.S+dy[j], sp[i]-1,i);
    				}
    		}
    	}
    }
    int ctr[11]={};
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		if (inp[i][j]>0){
    			ctr[inp[i][j]]++;
    		}
    	}
    }
    for (int i = 1; i<=p; i++ ){
    	cout<<ctr[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod = 1000000007;
int powe(int x, int y){ x = x%mod;int ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
int inp[1003][1003];
int now[1003][1003];
int sp[11];
int n,m,p;
bool fin;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
vector< pair<int,int> > pos[11];
void recur(int x, int y, int moves, int p, int dir){
	x+=dx[dir];
	y+=dy[dir];
	if (x >=n || x<0 || y>=m || y<0 || moves<=0)
		return;
	if ((inp[x][y]>0 && now[x][y]==0) || (inp[x][y] == -1))
		return;
	inp[x][y] = p;
	now[x][y] = 1;
	pos[p].PB(MP(x,y));
	recur(x,y,moves-1,p,dir);
	fin=false;
}
int main(){
    fast;
    cin>>n>>m>>p;
    for (int i=1; i<=p; i++){
    	cin>>sp[i];
    }
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		char a;
    		cin>>a;
    		if (a == '#')
    			inp[i][j] = -1;
    		else if (a == '.')
    			inp[i][j] = 0;
    		else{
    			inp[i][j] = a-'0';
    			pos[a-'0'].PB(MP(i,j));
    		}
    	}
    }
    fin = false;
    while(!fin){
    	fin = true;
    	for (int i = 1; i<=p; i++){
    		for (auto x:pos[i]){
    				for (int j = 0; j<4; j++){
    					recur(x.F, x.S, sp[i],i, j);
    				}
    		}
    		memset(now,0,sizeof(now));
    	}
    }
    int ctr[11]={};
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		if (inp[i][j]>0){
    			ctr[inp[i][j]]++;
    		}
    	}
    }
    for (int i = 1; i<=p; i++ ){
    	cout<<ctr[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod = 1000000007;
int powe(int x, int y){ x = x%mod;int ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
int inp[1003][1003];
int now[1003][1003];
int proc[1003][1003];
int sp[11];
int n,m,p;
bool fin;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
vector< pair<int,int> > pos[11];
void recur(int x, int y, int moves, int p){
	if (x >=n || x<0 || y>=m || y<0 || moves<0)
		return;
	if (proc[x][y]||(inp[x][y]>0 && inp[x][y]!=p) || (inp[x][y] == -1) || (inp[x][y] == p &&now[x][y]>=moves ))
		return;
	inp[x][y] = p;
	now[x][y] = moves;
	pos[p].PB(MP(x,y));
	for (int j = 0; j<4; j++)
		recur(x+dx[j],y+dy[j],moves-1,p);
	fin=false;
}
int main(){
    fast;
    cin>>n>>m>>p;
    for (int i=1; i<=p; i++){
    	cin>>sp[i];
    	sp[i] = min(sp[i],3000);
    }
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		char a;
    		cin>>a;
    		if (a == '#')
    			inp[i][j] = -1;
    		else if (a == '.')
    			inp[i][j] = 0;
    		else{
    			inp[i][j] = a-'0';
    			pos[a-'0'].PB(MP(i,j));
    		}
    	}
    }
    fin = false;
    while(!fin){
    	fin = true;
    	for (int i = 1; i<=p; i++){
    		for (auto x:pos[i]){
    				for (int j = 0; j<4; j++){
    					now[x.F][x.S]=sp[i];
    					proc[x.F][x.S]=1;
    					recur(x.F+dx[j], x.S+dy[j], sp[i]-1,i);
    				}
    		}
    	}
    }
    int ctr[11]={};
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		if (inp[i][j]>0){
    			ctr[inp[i][j]]++;
    		}
    	}
    }
    for (int i = 1; i<=p; i++ ){
    	cout<<ctr[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod = 1000000007;
int powe(int x, int y){ x = x%mod;int ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
int n,m,p;
string arr[1005];
int sp[10];
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int main(){
    fast;
    cin>>n>>m>>p;
    for (int i = 0; i<p; i++){
    	cin>>sp[i];
    	sp[i] = min(sp[i],2005);
    }
    for (int i = 0; i<n; i++)
    	cin>>arr[i];
    vector< pair<int,int> >cor[10],empty,temp;
    int ctr[10]={};
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		cout<<i<<" "<<j<<endl;
    		if (arr[i][j]== '.' || arr[i][j] == '#')
    			continue;
    		cor[arr[i][j]-'0'-1].PB({i,j});
    		ctr[arr[i][j]-'0'-1]++;
    	}
    }
    bool move = true;
    while(move){
    	move = false;
    	for (int i = 0; i<p; i++){
    		for (int l = 0; l<sp[i] && cor[i].size(); l++){
    		for (int a = cor[i].size()-1; a>=0; a--){
    			int x=cor[i][a].F, y = cor[i][a].S;
    			for (int k = 0; k<4; k++){
    				if (x+dx[k] < n && x+dx[k]>=0 && y+dy[k]>=0 && y+dy[k]<m){
    					if (arr[x+dx[k]][y+dy[k]] == '.'){
    						arr[x+dx[k]][y+dy[k]] = '1'+i;
    						temp.PB({x+dx[k],y+dy[k]});
    						ctr[i]++;
    						move = true;
    					}
    				}
    			}
    		}
    		cor[i]=temp;
    		temp = empty;
    		}
    	}
    }
    for (int i = 0; i<p; i++){
    	cout<<ctr[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod = 1000000007;
int powe(int x, int y){ x = x%mod;int ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
int n,m,p;
string arr[1005];
int sp[10];
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int main(){
    fast;
    cin>>n>>m>>p;
    for (int i = 0; i<p; i++){
    	cin>>sp[i];
    }
    for (int i = 0; i<n; i++)
    	cin>>arr[i];
    vector< pair<int,int> >cor[p];
    int ptr[p][2]={};
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		if (arr[i][j]== '.' || arr[i][j] == '#'){
    			continue;
    		}
    		cor[arr[i][j]-'0'-1].PB({i,j});
    		ptr[arr[i][j]-'0'-1][1]++;
    	}
    }
    bool move = true;
    while(move){
    	move = false;
    	for (int i = 0; i<p; i++){
    		for (int l = 0; l<sp[i]; l++){
    			ptr[i][1] = cor[i].size();
    		for (; ptr[i][0]<ptr[i][1]; ptr[i][0]++){
    			int x=cor[i][ptr[i][0]].F, y = cor[i][ptr[i][0]].S;
    			for (int k = 0; k<4; k++){
    				if (x+dx[k] < n && x+dx[k]>=0 && y+dy[k]>=0 && y+dy[k]<m){
    					if (arr[x+dx[k]][y+dy[k]] == '.'){
    						arr[x+dx[k]][y+dy[k]] = '1'+i;
    						cor[i].PB({x+dx[k],y+dy[k]});
    						cout<<i<<" - ("<<x+dx[k]<<","<<y+dy[k]<<")\n";
    						move = true;
    					}
    				}
    			}
    		}
    		}
    	}
    }
    for (int i = 0; i<p; i++){
    	cout<<cor[i].size()<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod = 1000000007;
int powe(int x, int y){ x = x%mod;int ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
int inp[1003][1003];
int now[1003][1003];
int proc[1003][1003];
int sp[11];
int n,m,p;
bool fin;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
vector< pair<int,int> > pos[11];
void recur(int x, int y, int moves, int p, int dir){
	if (x >=n || x<0 || y>=m || y<0 || moves<0)
		return;
	if (proc[x][y]||(inp[x][y]>0 && inp[x][y]!=p) || (inp[x][y] == -1) || (inp[x][y] == p &&now[x][y]>=moves ))
		return;
	inp[x][y] = p;
	now[x][y] = moves;
	pos[p].PB(MP(x,y));
	for (int j = 0; j<4; j++)
		recur(x+dx[j],y+dy[j],moves-1,p,j);
	fin=false;
}
int main(){
    fast;
    cin>>n>>m>>p;
    for (int i=1; i<=p; i++){
    	cin>>sp[i];
    }
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		char a;
    		cin>>a;
    		if (a == '#')
    			inp[i][j] = -1;
    		else if (a == '.')
    			inp[i][j] = 0;
    		else{
    			inp[i][j] = a-'0';
    			pos[a-'0'].PB(MP(i,j));
    		}
    	}
    }
    fin = false;
    while(!fin){
    	fin = true;
    	for (int i = 1; i<=p; i++){
    		for (auto x:pos[i]){
    				for (int j = 0; j<4; j++){
    					now[x.F][x.S]=sp[i];
    					proc[x.F][x.S]=1;
    					recur(x.F+dx[j], x.S+dy[j], sp[i]-1,i, j);
    				}
    		}
    	}
    }
    int ctr[11]={};
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		if (inp[i][j]>0){
    			ctr[inp[i][j]]++;
    		}
    	}
    }
    for (int i = 1; i<=p; i++ ){
    	cout<<ctr[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod = 1000000007;
int powe(int x, int y){ x = x%mod;int ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
int inp[1003][1003];
int now[1003][1003];
int sp[11];
int n,m,p;
bool fin;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
vector< pair<int,int> > pos[11];
void recur(int x, int y, int moves, int p, int dir){
	if (x >=n || x<0 || y>=m || y<0 || moves<0)
		return;
	if ((inp[x][y]>0 && inp[x][y]!=p) || (inp[x][y] == -1) || (inp[x][y] == p && now[x][y]>=moves))
		return;
	inp[x][y] = p;
	now[x][y] = moves;
	pos[p].PB(MP(x,y));
	for (int j = 0; j<4; j++)
		recur(x+dx[j],y+dy[j],moves-1,p,j);
	fin=false;
}
int main(){
    fast;
    cin>>n>>m>>p;
    for (int i=1; i<=p; i++){
    	cin>>sp[i];
    }
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		char a;
    		cin>>a;
    		if (a == '#')
    			inp[i][j] = -1;
    		else if (a == '.')
    			inp[i][j] = 0;
    		else{
    			inp[i][j] = a-'0';
    			pos[a-'0'].PB(MP(i,j));
    		}
    	}
    }
    fin = false;
    while(!fin){
    	fin = true;
    	for (int i = 1; i<=p; i++){
    		for (auto x:pos[i]){
    				for (int j = 0; j<4; j++){
    					recur(x.F+dx[j], x.S+dy[j], sp[i]-1,i, j);
    				}
    		}
    		memset(now,0,sizeof(now));
    	}
    }
    int ctr[11]={};
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		//cout<<inp[i][j]<<" ";
    		if (inp[i][j]>0){
    			ctr[inp[i][j]]++;
    		}
    	}
    	//cout<<endl;
    }
    for (int i = 1; i<=p; i++ ){
    	cout<<ctr[i]<<" ";
    }
    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<pair<long long,long long> > vpll;
typedef vector<vector<pair<ll,ll> > > vvpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;

#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define all(x) x.begin() , x.end()
#define pb push_back
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define left (node << 1)
#define right (node << 1) + 1
#define pii pair<int,int>
#define mii map<int,int>
#define vi vector<int>
#define umii unordered_map<int,int>
#define mod 1000000007
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<": " <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define repo(i,o,e) for(int i = o ; i < e ; i++)
#define rep(n) for(int i = 0 ; i < n ; i++)
const ll sz = 1e3+5;
char mat[sz][sz];
int s[10];
int n , m , p;
map<pii,int> vis;
map<char,stack<pii>> Map;
void bfs(int x , int y , int player){
    int speed = s[player-1];
    int distance = 0;
    queue<pair<int,pii>> Q;
    Q.push({distance,{x,y}});
    while(!Q.empty()){
        pair<int,pii> temp = Q.front();
        Q.pop();
        int dis = temp.F;
        int i = temp.S.F;
        int j = temp.S.S;
        if(dis == speed){
            Map[char(48+player)].push({i,j});
        }
        if(dis > speed){
            break;
        }
        mat[i][j] = char(48+player);
        vis[{i,j}] = 1;
        if(i+1 < n and mat[i+1][j] == '.' and !vis[{i+1,j}]){
            Q.push({dis+1,{i+1,j}});
        }
        if(i-1 >= 0 and mat[i-1][j] == '.' and !vis[{i-1,j}]){
            Q.push({dis+1,{i-1,j}});
        }
        if(j+1 < m and mat[i][j+1] == '.' and !vis[{i,j+1}]){
            Q.push({dis+1,{i,j+1}});
        }
        if(j-1 >= 0 and mat[i][j-1] == '.' and !vis[{i,j-1}]){
            Q.push({dis+1,{i,j-1}});
        }
    }
}

void solve(){
    cin >> n >> m >> p;
    rep(p){
        cin >> s[i];
        s[i] = min(s[i],2005);
    }
    rep(n){
        repo(j,0,m){
            cin >> mat[i][j];
            int ascii = int(mat[i][j])-48;
            if(ascii > 0 and ascii < 10){
                Map[mat[i][j]].push({i,j});
            }
        }
    }
    bool flag = true;
    while(true){
        if(flag == false)break;
        flag = false;
        for(auto &i:Map){
            stack<pii> temps;
            while(!i.S.empty()){
                flag = true;
                pii temporary = i.S.top();
                temps.push(temporary);
                i.S.pop();
            }
            while(!temps.empty()){
                pii temporary = temps.top();
                bfs(temporary.F,temporary.S,int(i.F)-48);
                temps.pop();
            }
        }
    }
}
int HASH[10];
int main(){
    FIO;
    int t=1;
    while(t--){
        solve();
    }
    rep(n){
        repo(j,0,m){
            int ascii = int(mat[i][j]) - 48;
            if(ascii > 0 and ascii < 10){
                HASH[ascii]++;
            }
        }
    }
    for(int i = 1 ; i <= p ; i++)cout << HASH[i] <<  " ";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod = 1000000007;
int powe(int x, int y){ x = x%mod;int ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
int inp[1003][1003];
int now[1003][1003];
int sp[11];
int n,m,p;
bool fin;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
vector< pair<int,int> > pos[11];
void recur(int x, int y, int moves, int p, int dir){
	if (x >=n || x<0 || y>=m || y<0 || moves<0)
		return;
	if ((inp[x][y]>0 && inp[x][y]!=p) || (inp[x][y] == -1) || (inp[x][y] == p && (now[x][y]>=moves || now[x][y]==-1) ))
		return;
	inp[x][y] = p;
	now[x][y] = moves;
	pos[p].PB(MP(x,y));
	for (int j = 0; j<4; j++)
		recur(x+dx[j],y+dy[j],moves-1,p,j);
	fin=false;
}
int main(){
    fast;
    cin>>n>>m>>p;
    for (int i=1; i<=p; i++){
    	cin>>sp[i];
    }
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		char a;
    		cin>>a;
    		if (a == '#')
    			inp[i][j] = -1;
    		else if (a == '.')
    			inp[i][j] = 0;
    		else{
    			inp[i][j] = a-'0';
    			pos[a-'0'].PB(MP(i,j));
    		}
    	}
    }
    fin = false;
    while(!fin){
    	fin = true;
    	memset(now,-1,sizeof(now));
    	for (int i = 1; i<=p; i++){
    		for (auto x:pos[i]){
    				for (int j = 0; j<4; j++){
    					recur(x.F+dx[j], x.S+dy[j], sp[i]-1,i, j);
    				}
    		}
    	}
    }
    int ctr[11]={};
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		if (inp[i][j]>0){
    			ctr[inp[i][j]]++;
    		}
    	}
    }
    for (int i = 1; i<=p; i++ ){
    	cout<<ctr[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod = 1000000007;
int powe(int x, int y){ x = x%mod;int ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
int inp[1003][1003];
int now[1003][1003];
int sp[11];
int n,m,p;
bool fin;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
vector< pair<int,int> > pos[11];
void recur(int x, int y, int moves, int p, int dir){
	if (x >=n || x<0 || y>=m || y<0 || moves<0)
		return;
	if ((inp[x][y]>0 && inp[x][y]!=p) || (inp[x][y] == -1) || (inp[x][y] == p &&now[x][y]>=moves ))
		return;
	inp[x][y] = p;
	now[x][y] = moves;
	pos[p].PB(MP(x,y));
	for (int j = 0; j<4; j++)
		recur(x+dx[j],y+dy[j],moves-1,p,j);
	fin=false;
}
int main(){
    fast;
    cin>>n>>m>>p;
    for (int i=1; i<=p; i++){
    	cin>>sp[i];
    }
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		char a;
    		cin>>a;
    		if (a == '#')
    			inp[i][j] = -1;
    		else if (a == '.')
    			inp[i][j] = 0;
    		else{
    			inp[i][j] = a-'0';
    			pos[a-'0'].PB(MP(i,j));
    		}
    	}
    }
    fin = false;
    while(!fin){
    	fin = true;
    	for (int i = 1; i<=p; i++){
    		for (auto x:pos[i]){
    				for (int j = 0; j<4; j++){
    					now[x.F][x.S]=sp[i];
    					recur(x.F+dx[j], x.S+dy[j], sp[i]-1,i, j);
    				}
    		}
    	}
    }
    int ctr[11]={};
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		if (inp[i][j]>0){
    			ctr[inp[i][j]]++;
    		}
    	}
    }
    for (int i = 1; i<=p; i++ ){
    	cout<<ctr[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod = 1000000007;
int powe(int x, int y){ x = x%mod;int ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
int n,m,p;
string arr[1005];
int sp[10];
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int main(){
    fast;
    cin>>n>>m>>p;
    for (int i = 0; i<p; i++){
    	cin>>sp[i];
    }
    for (int i = 0; i<n; i++)
    	cin>>arr[i];
    vector< pair<int,int> >cor[p];
    int ptr[p][2]={};
    for (int i = 0; i<n; i++){
    	for (int j = 0; j<m; j++){
    		if (arr[i][j]== '.' || arr[i][j] == '#'){
    			continue;
    		}
    		cor[arr[i][j]-'0'-1].PB({i,j});
    		ptr[arr[i][j]-'0'-1][1]++;
    	}
    }
    bool move = true;
    while(move){
    	move = false;
    	for (int i = 0; i<p; i++){
    		for (int l = 0; l<sp[i]; l++){
    			ptr[i][1] = cor[i].size();
    		for (; ptr[i][0]<ptr[i][1]; ptr[i][0]++){
    			int x=cor[i][ptr[i][0]].F, y = cor[i][ptr[i][0]].S;
    			for (int k = 0; k<4; k++){
    				if (x+dx[k] < n && x+dx[k]>=0 && y+dy[k]>=0 && y+dy[k]<m){
    					if (arr[x+dx[k]][y+dy[k]] == '.'){
    						arr[x+dx[k]][y+dy[k]] = '1'+i;
    						cor[i].PB({x+dx[k],y+dy[k]});
    						move = true;
    					}
    				}
    			}
    		}
    		}
    	}
    }
    for (int i = 0; i<p; i++){
    	cout<<cor[i].size()<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int max(int a, int b) { return (a > b) ? a : b; }

const int MAXN = 300000;
const int MOD = (int) 1e9+7;
int a[MAXN];

const int INF = 1e9;

char c[1000][1000];
int dist[10][1000][1000];
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

bool inField(int ni, int nj, int n, int m);

void bfs(int x, int n, int m)
{
    deque<int> q;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (c[i][j] == '#') dist[x][i][j] = -1;
            if (c[i][j] == '0' + x)
            {
                dist[x][i][j] = 0;
                q.push_back(i);
                q.push_back(j);
            }
        }
    }

    while (!q.empty())
    {
        int i = q.front(); q.pop_front();
        int j = q.front(); q.pop_front();
        for (int v = 0; v < 4; v++)
        {
            int ni = i + dx[v];
            int nj = j + dy[v];
            if (inField(ni, nj, n, m) && dist[x][ni][nj] > dist[x][i][j] + 1)
            {
                dist[x][ni][nj] = dist[x][i][j] + 1;
                q.push_back(ni);
                q.push_back(nj);
            }
        }
    }
}

bool inField(int ni, int nj, int n, int m) {
    return (ni >= 0 && nj >= 0 && ni < n && nj < m);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, p;
    cin >> n >> m >> p;
    for (int i = 1; i  <= p; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        fgets(c[i], 1000, stdin);
    }

    for (int q = 1; q <= p; q++)
    {
        for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            dist[q][i][j] = INF;
        bfs(q, n, m);
//        for (int i = 0; i < n; i++)
//            for (int j = 0; j < m; j++)
//                cerr << dist[q][i][j] << " \n"[j == m - 1];
    }
    vector<int> ans(10, 0);
    for (int i = 0; i < 10; i++)
    {
        ans[i] = 0;
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (c[i][j] == '#') continue;
            if (c[i][j] != '.') ans[c[i][j] - '0']++;
            if (c[i][j] == '.')
            {
                int z = INF;
                int zz = -1;
                for (int q = 1; q <= p; q++) {
                    if ((dist[q][i][j] - 1) / a[q] + 1 < z) {
                        z = (dist[q][i][j] - 1) / a[q] + 1;
                        zz = q;
                    }
                }
                ans[zz]++;
            }
        }
    }
    for (int i = 1; i <= p; i++)
    {
        cout << ans[i] << " ";
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int max(int a, int b)
{ return (a > b) ? a : b; }

const int MAXN = 1000;
char c[MAXN][MAXN];
int a[10];

int n, m;
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

bool inField(int ni, int nj);

void bfs(deque<int>& deque1, int x, int y)
{
    int color = c[x][y] - '0';
    int step = a[color];
    deque<int> q;
    q.push_back(x);
    q.push_back(y);
    q.push_back(0);
    while (!q.empty())
    {
        int i = q.front(); q.pop_front();
        int j = q.front(); q.pop_front();
        int s = q.front(); q.pop_front();
        for (int v = 0; v < 4; v++)
        {
            int ni = i + dx[v];
            int nj = j + dy[v];
            int ns = s + 1;
            if (inField(ni, nj) && c[ni][nj] == '.')
            {
                c[ni][nj] = c[x][y];
                if (ns == step)
                {
                    deque1.push_back(ni);
                    deque1.push_back(nj);
                } else {
                    q.push_back(ni);
                    q.push_back(nj);
                    q.push_back(ns);
                }
            }
        }
    }
}

bool inField(int ni, int nj) {
    return (ni >= 0 && nj >= 0 && ni < n && nj < m);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int p;
    cin >> n >> m >> p;
    for (int i = 1; i <= p; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        string s;
        cin >> s;
        for (int j = 0; j  <m; j++)
        {
            c[i][j] = s[j];
        }
    }
    deque<int> q;
    for (int qq = 1; qq <= p; qq++)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (c[i][j] == '0' + qq)
                {
                    q.push_back(i);
                    q.push_back(j);
                }
            }
        }
    }

    while (!q.empty())
    {
        int i = q.front(); q.pop_front();
        int j = q.front(); q.pop_front();
        bfs(q, i, j);
    }

    vector<int> ans(10, 0);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (c[i][j] != '#')
                ans[c[i][j] - '0']++;
        }
    }
    for (int q = 1; q <= p; q++)
    {
        cout << ans[q] << " ";
    }

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int max(int a, int b) { return (a > b) ? a : b; }

const int MAXN = 300000;
const int MOD = (int) 1e9+7;
int a[MAXN];

const int INF = 1e9;

char c[1000][1000];
int dist[10][1000][1000];
int pl[1000][1000][10];

bool ok[1000][1000];
int cur[1000][1000];
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

bool inField(int ni, int nj, int n, int m);

void bbfs(int n, int m)
{
    deque<int> q;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (c[i][j] == '#') continue;
            if (ok[i][j])
            {
                q.push_back(i);
                q.push_back(j);
            }
        }
    }

    while (!q.empty())
    {
        int i = q.front(); q.pop_front();
        int j = q.front(); q.pop_front();
        for (int v = 0; v < 4; v++)
        {
            int ni = i + dx[v];
            int nj = j + dy[v];
            if (inField(ni, nj, n, m) && !ok[ni][nj] && cur[ni][nj] == cur[i][j])
            {
                ok[ni][nj] = 1;
                q.push_back(ni);
                q.push_back(nj);
            }
        }
    }
}
void bfs(int x, int n, int m)
{
    deque<int> q;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (c[i][j] == '#') dist[x][i][j] = -1;
            if (c[i][j] == '0' + x)
            {
                dist[x][i][j] = 0;
                q.push_back(i);
                q.push_back(j);
            }
        }
    }

    while (!q.empty())
    {
        int i = q.front(); q.pop_front();
        int j = q.front(); q.pop_front();
        for (int v = 0; v < 4; v++)
        {
            int ni = i + dx[v];
            int nj = j + dy[v];
            if (inField(ni, nj, n, m) && dist[x][ni][nj] > dist[x][i][j] + 1)
            {
                dist[x][ni][nj] = dist[x][i][j] + 1;
                q.push_back(ni);
                q.push_back(nj);
            }
        }
    }
}

bool inField(int ni, int nj, int n, int m) {
    return (ni >= 0 && nj >= 0 && ni < n && nj < m);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, p;
    cin >> n >> m >> p;
    for (int i = 1; i  <= p; i++)
    {
        cin >> a[i];
    }
    string s;
    for (int i = 0; i < n; i++)
    {
        cin >> s;
        for (int j = 0; j < m; j++)
        {
            c[i][j] = s[j];
        }
//        c[i] = s.c_str();
//        fgets(c[i], 1000, stdin);
    }

    for (int q = 1; q <= p; q++)
    {
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                dist[q][i][j] = INF;
        bfs(q, n, m);
//        for (int i = 0; i < n; i++)
//            for (int j = 0; j < m; j++)
//                cerr << dist[q][i][j] << " \n"[j == m - 1];
    }
    vector<int> ans(10, 0);
    for (int i = 0; i < 10; i++)
    {
        ans[i] = 0;
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
//            cout << c[i][i] << " \n"[j == m - 1];
            if (c[i][j] == '#')
            {
                cur[i][j] = 0;
                ok[i][j] = true;
                continue;
            }
            if (c[i][j] != '.')
            {
                ok[i][j] = true;
                cur[i][j] = c[i][j] - '0';
            }
            if (c[i][j] == '.')
            {
                for (int q = 1; q <= p; q++) {
                    pl[i][j][q - 1] = q;
                }
                stable_sort(pl[i][j], pl[i][j] + p , [i, j](const int x, const int y)
                {
                    return ((dist[x][i][j] - 1) / a[x] < (dist[y][i][j] - 1) / a[y]);
                });
                cur[i][j] = pl[i][j][0];
            }
        }
    }

    for (int q = 0; q < p; q++)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (!ok[i][j])
                {
                    cur[i][j] = pl[i][j][q];
                }
            }
        }
        bbfs(n, m);
    }
//    cout << n <<" " <<  m << " ";

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            ans[cur[i][j]] += 1;
        }
    }

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