//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    while(cnt<(n*m)&&tl<=100)
    {
        for(int i=0;i<n;i++)
            s1[i]=st[i];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(b[j][k]<=s[i])
                        st[j][k]=ch;
                   if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
    // int a[n][m];
    // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    vector<pair<int,int> > vr[p+1];
    for(int i=1;i<=p;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<m;k++)
            {
                if(st[j][k]!='.'&&st[j][k]!='#')
                {
                    vr[st[j][k]-'0'].pb(m1(j,k));
                }
            }
        }
    }

    while(cnt<(n*m)&&tl<=200)
    {
        for(int i=0;i<n;i++)
            s1[i]=st[i];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];
             bool visited[n][m];memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int l=0;l<vr[i].size();l++)
                    {
                        int j=vr[i][l].first;
                        int k=vr[i][l].second;
                        b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                        // if(i==2)
                        //{
                        //     cout<<j<<" "<<k<<endl;
                        // }
                    }
            vr[i].clear();
            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(b[i1][j1]<=s[i]&&i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(b[i1][j1]<=s[i]&&i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                    {st[j][k]=ch;
                        vr[i].pb(m1(j,k));
                    }
                    if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
    // int a[n][m];
    // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    vector<pair<int,int> > vr[p+1];
    for(int i=1;i<=p;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<m;k++)
            {
                if(st[j][k]!='.'&&st[j][k]!='#')
                {
                    vr[st[j][k]-'0'].pb(m1(j,k));
                }
            }
        }
    }

    while(cnt<(n*m)&&tl<=325)
    {
        for(int i=0;i<n;i++)
            s1[i]=st[i];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];
             bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int l=0;l<vr[i].size();l++)
                    {
                        int j=vr[i][l].first;
                        int k=vr[i][l].second;
                        b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                        // if(i==2)
                        //{
                        //     cout<<j<<" "<<k<<endl;
                        // }
                    }
            vr[i].clear();
            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(b[i1][j1]<=s[i]&&i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(b[i1][j1]<=s[i]&&i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                    {st[j][k]=ch;
                        vr[i].pb(m1(j,k));
                    }
                    if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
    // int a[n][m];
    // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    vector<pair<int,int> > vr[p+1];
    for(int i=1;i<=p;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<m;k++)
            {
                if(st[j][k]!='.'&&st[j][k]!='#')
                {
                    vr[st[j][k]-'0'].pb(m1(j,k));
                }
            }
        }
    }

    while(cnt<(n*m)&&tl<=350)
    {
        for(int i=0;i<n;i++)
            s1[i]=st[i];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];
             bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int l=0;l<vr[i].size();l++)
                    {
                        int j=vr[i][l].first;
                        int k=vr[i][l].second;
                        b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                        // if(i==2)
                        //{
                        //     cout<<j<<" "<<k<<endl;
                        // }
                    }
            vr[i].clear();
            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(b[i1][j1]<=s[i]-1&&i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(b[i1][j1]<=s[i]-1&&j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(b[i1][j1]<=s[i]-1&&i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(b[i1][j1]<=s[i]-1&&j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                    {st[j][k]=ch;
                        vr[i].pb(m1(j,k));
                    }
                    if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
const int INF =100000000;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
int main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;
    while(cnt<(n*m))
    {
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&st[i1-1][j1]=='.')
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&st[i1][j1-1]=='.')
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&st[i1+1][j1]=='.')
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&st[i1][j1+1]=='.')
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                        st[j][k]=ch;
                    if(st[j][k]!='.')
                        cnt++;
                }
            }

        }
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    while(cnt<(n*m)&&tl<=200)
    {
        for(int i=0;i<n;i++)
            s1[i]=st[i];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(b[j][k]<=s[i])
                        st[j][k]=ch;
                   if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
    // int a[n][m];
    // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    vector<pair<int,int> > vr[p+1];
    for(int i=1;i<=p;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<m;k++)
            {
                if(st[j][k]!='.'&&st[j][k]!='#')
                {
                    vr[st[j][k]-'0'].pb(m1(j,k));
                }
            }
        }
    }

    while(cnt<(n*m)&&tl<=400)
    {
        for(int i=0;i<n;i++)
            s1[i]=st[i];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];
             bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int l=0;l<vr[i].size();l++)
                    {
                        int j=vr[i][l].first;
                        int k=vr[i][l].second;
                        b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                        // if(i==2)
                        //{
                        //     cout<<j<<" "<<k<<endl;
                        // }
                    }
            vr[i].clear();
            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(b[i1][j1]<=s[i]&&i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(b[i1][j1]<=s[i]&&i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                    {st[j][k]=ch;
                        vr[i].pb(m1(j,k));
                    }
                    if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
const int MAXN = 1005;
const int MAXM = 1005;
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1,  0, 0, 1, -1, 1, -1};

int A[MAXN][MAXM], S[10], N, M, P;
deque<ti> fringe[10];
int ans[10];

void bfs(int player, int turn){
    int orgdist = turn * S[player];
    while (!fringe[player].empty()){
        int curx = get<0>(fringe[player].front());
        int cury = get<1>(fringe[player].front());
        int curdist = get<2>(fringe[player].front());
        if (curdist == orgdist) break;
        fringe[player].pop_front();
        for (int i=0; i<4; i++){
            int nx = curx + dx[i], ny = cury + dy[i];
            if (nx < 0 || ny < 0) continue;
            if (nx >= N || ny >= M) continue;
            if (A[nx][ny] != 0) continue;
            if (curdist + 1 > orgdist) continue;
            fringe[player].push_back(ti(nx, ny, curdist+1));
            A[nx][ny] = player;
        }
    }
}

void printfringe(int player){
    for (auto i:fringe[player]){
        cout << get<0>(i) << "," << get<1>(i) << "," << get<2>(i) << " ";
    }
    cout << "\n";
}
bool isallempty(){
    bool res = 1;
    for (int i=0; i<10; i++){
        if (fringe[i].size() > 0) res = 0;
    }
    return res;
}

main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> N >> M >> P;
    for (int i=1; i<=P; i++) cin >> S[i];
    for (int i=0; i<N; i++){
        for (int j=0; j<M; j++){
            char c; cin >> c;
            if (c == '.') continue;
            if (c == '#') A[i][j] = -1;
            else{
                int pnum = (int) c - '0';
                A[i][j] = pnum;
                fringe[pnum].push_back(ti(i,j,0));
            }
        }
    }
    int cnt = 0;
    while (!isallempty()){
        cnt++;
        for (int i=1; i<=P; i++){
            bfs(i, cnt);
        }
    }
    /*
    for (int i=0; i<N; i++){
        for (int j=0; j<M; j++){
            cout << A[i][j] << " ";
        }
        cout << "\n";
    }
    */
    for (int i=0; i<N; i++){
        for (int j=0; j<M; j++){
            if (A[i][j] > 0){
                ans[A[i][j]]++;
            }
        }
    }
    for (int i=1; i<=P; i++){
        cout << ans[i] << " ";
    }
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    while(tl<=40)
    {
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'||st[i1-1][j1]==ch))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'||st[i1][j1-1]==ch))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'||st[i1+1][j1]==ch))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'||st[i1][j1+1]==ch))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(b[j][k]<=s[i])
                        st[j][k]=ch;
                   // if(st[j][k]!='.')
                     //   cnt++;
                }
            }
            //if(cnt>=(n*m))
               // break;

        }
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    while(tl<=30)
    {
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'||st[i1-1][j1]==ch))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'||st[i1][j1-1]==ch))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'||st[i1+1][j1]==ch))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'||st[i1][j1+1]==ch))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(b[j][k]<=s[i])
                        st[j][k]=ch;
                   // if(st[j][k]!='.')
                     //   cnt++;
                }
            }
            //if(cnt>=(n*m))
               // break;

        }
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
    // int a[n][m];
    // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    vector<pair<int,int> > vr[p+1];
    for(int i=1;i<=p;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<m;k++)
            {
                if(st[j][k]!='.'&&st[j][k]!='#')
                {
                    vr[st[j][k]-'0'].pb(m1(j,k));
                }
            }
        }
    }

    while(cnt<(n*m)&&tl<=300)
    {
        for(int i=0;i<n;i++)
            s1[i]=st[i];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];
             bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int l=0;l<vr[i].size();l++)
                    {
                        int j=vr[i][l].first;
                        int k=vr[i][l].second;
                        b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                        // if(i==2)
                        //{
                        //     cout<<j<<" "<<k<<endl;
                        // }
                    }
            vr[i].clear();
            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(b[i1][j1]<=s[i]&&i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(b[i1][j1]<=s[i]&&i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                    {st[j][k]=ch;
                        vr[i].pb(m1(j,k));
                    }
                    if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    while(tl<=30)
    {
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'||st[i1-1][j1]==ch))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'||st[i1][j1-1]==ch))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'||st[i1+1][j1]==ch))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'||st[i1][j1+1]==ch))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                        st[j][k]=ch;
                   // if(st[j][k]!='.')
                     //   cnt++;
                }
            }
            //if(cnt>=(n*m))
               // break;

        }
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
    // int a[n][m];
    // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    vector<pair<int,int> > vr[p+1];
    for(int i=1;i<=p;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<m;k++)
            {
                if(st[j][k]!='.'&&st[j][k]!='#')
                {
                    vr[st[j][k]-'0'].pb(m1(j,k));
                }
            }
        }
    }
    while(cnt<(n*m)&&tl<=200)
    {
        for(int i=0;i<n;i++)
            s1[i]=st[i];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int l=0;l<vr[i].size();l++)
                    {
                        int j=vr[i][l].first;
                        int k=vr[i][l].second;
                        b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                        // if(i==2)
                        //{
                        //     cout<<j<<" "<<k<<endl;
                        // }
                    }
            vr[i].clear();
            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                    {st[j][k]=ch;
                        vr[i].pb(m1(j,k));
                    }
                    if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    while(cnt<(n*m))
    {
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                s1[i][j]=st[i][j];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'||st[i1-1][j1]==ch))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'||st[i1][j1-1]==ch))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'||st[i1+1][j1]==ch))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'||st[i1][j1+1]==ch))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(b[j][k]<=s[i])
                        st[j][k]=ch;
                   if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        //tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
const int INF =1100000000;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
int main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;
    while(cnt<(n*m))
    {
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'||st[i1-1][j1]==ch))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'||st[i1][j1-1]==ch))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'||st[i1+1][j1]==ch))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'||st[i1][j1+1]==ch))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                        st[j][k]=ch;
                    if(st[j][k]!='.')
                        cnt++;
                }
            }

        }
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
    // int a[n][m];
    // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    vector<pair<int,int> > vr[p+1];
    for(int i=1;i<=p;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<m;k++)
            {
                if(st[j][k]!='.'&&st[j][k]!='#')
                {
                    vr[st[j][k]-'0'].pb(m1(j,k));
                }
            }
        }
    }

    while(cnt<(n*m)&&tl<=350)
    {
        for(int i=0;i<n;i++)
            s1[i]=st[i];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];
             bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int l=0;l<vr[i].size();l++)
                    {
                        int j=vr[i][l].first;
                        int k=vr[i][l].second;
                        b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                        // if(i==2)
                        //{
                        //     cout<<j<<" "<<k<<endl;
                        // }
                    }
            vr[i].clear();
            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(b[i1][j1]<=s[i]&&i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(b[i1][j1]<=s[i]&&i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                    {st[j][k]=ch;
                        vr[i].pb(m1(j,k));
                    }
                    if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
const int MAXN = 1005;
const int MAXM = 1005;
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1,  0, 0, 1, -1, 1, -1};

int A[MAXN][MAXM], S[10], N, M, P;
deque<ti> fringe[10];
int ans[10];

void bfs(int player, int turn){
    int orgdist = turn * S[player];
    while (!fringe[player].empty()){
        int curx = get<0>(fringe[player].front());
        int cury = get<1>(fringe[player].front());
        int curdist = get<2>(fringe[player].front());
        if (curdist == orgdist) break;
        fringe[player].pop_front();
        for (int i=0; i<4; i++){
            int nx = curx + dx[i], ny = cury + dy[i];
            if (nx < 0 || ny < 0) continue;
            if (nx >= N || ny >= M) continue;
            if (A[nx][ny] != 0) continue;
            if (curdist + 1 > orgdist) continue;
            fringe[player].push_back(ti(nx, ny, curdist+1));
            A[nx][ny] = player;
        }
    }
}

void printfringe(int player){
    for (auto i:fringe[player]){
        cout << get<0>(i) << "," << get<1>(i) << "," << get<2>(i) << " ";
    }
    cout << "\n";
}
bool isallempty(){
    bool res = 1;
    for (int i=0; i<10; i++){
        if (fringe[i].size() > 0) res = 0;
    }
    return res;
}

main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> N >> M >> P;
    for (int i=1; i<=P; i++) cin >> S[i];
    for (int i=0; i<N; i++){
        for (int j=0; j<M; j++){
            char c; cin >> c;
            if (c == '.') continue;
            if (c == '#') A[i][j] = -1;
            else{
                int pnum = (int) c - '0';
                A[i][j] = pnum;
                fringe[pnum].push_back(ti(i,j,0));
            }
        }
    }
    int cnt = 0;
    while (!isallempty()){
        cnt++;
        for (int i=1; i<=P; i++){
            bfs(i, cnt);
        }
    }
    /*
    for (int i=0; i<N; i++){
        for (int j=0; j<M; j++){
            cout << A[i][j] << " ";
        }
        cout << "\n";
    }
    */
    for (int i=0; i<N; i++){
        for (int j=0; j<M; j++){
            if (A[i][j] > 0){
                ans[A[i][j]]++;
            }
        }
    }
    for (int i=1; i<=P; i++){
        cout << ans[i] << " ";
    }


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
const int INF =1010000000;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
int main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    while(cnt<(n*m)&&tl<=35)
    {
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'||st[i1-1][j1]==ch))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'||st[i1][j1-1]==ch))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'||st[i1+1][j1]==ch))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'||st[i1][j1+1]==ch))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                        st[j][k]=ch;
                    if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
const int INF =1100000000;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
int main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    while(cnt<(n*m)&&tl<=26)
    {
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                        st[j][k]=ch;
                    if(st[j][k]!='.')
                        cnt++;
                }
            }

        }
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
    // int a[n][m];
    // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    vector<pair<int,int> > vr[p+1];
    for(int i=1;i<=p;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<m;k++)
            {
                if(st[j][k]!='.'&&st[j][k]!='#')
                {
                    vr[st[j][k]-'0'].pb(m1(j,k));
                }
            }
        }
    }

    while(cnt<(n*m))
    {
        for(int i=0;i<n;i++)
            s1[i]=st[i];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];
             bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int l=0;l<vr[i].size();l++)
                    {
                        int j=vr[i][l].first;
                        int k=vr[i][l].second;
                        b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                        // if(i==2)
                        //{
                        //     cout<<j<<" "<<k<<endl;
                        // }
                    }
            vr[i].clear();
            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(b[i1][j1]<=s[i]&&i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(b[i1][j1]<=s[i]&&i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                    {st[j][k]=ch;
                        vr[i].pb(m1(j,k));
                    }
                    if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    while(cnt<(n*m))
    {
        for(int i=0;i<n;i++)
            s1[i]=st[i];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'||st[i1-1][j1]==ch))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'||st[i1][j1-1]==ch))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'||st[i1+1][j1]==ch))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'||st[i1][j1+1]==ch))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(b[j][k]<=s[i])
                        st[j][k]=ch;
                   if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        //tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
    // int a[n][m];
    // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    string s1[n];
    vector<pair<int,int> > vr[p+1];
    for(int i=1;i<=p;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<m;k++)
            {
                if(st[j][k]!='.'&&st[j][k]!='#')
                {
                    vr[st[j][k]-'0'].pb(m1(j,k));
                }
            }
        }
    }

    while(cnt<(n*m)&&tl<=500)
    {
        for(int i=0;i<n;i++)
            s1[i]=st[i];
        for(int i=1;i<=p;i++)
        {
            int b[n][m];
             bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int l=0;l<vr[i].size();l++)
                    {
                        int j=vr[i][l].first;
                        int k=vr[i][l].second;
                        b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                        // if(i==2)
                        //{
                        //     cout<<j<<" "<<k<<endl;
                        // }
                    }
            vr[i].clear();
            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(b[i1][j1]<=s[i]&&i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(b[i1][j1]<=s[i]&&i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(b[i1][j1]<=s[i]&&j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                    {st[j][k]=ch;
                        vr[i].pb(m1(j,k));
                    }
                    if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        int z=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(s1[i][j]!=st[i][j])
                {z=1;
                    break;
                }
            }
        if(!z)
            break;
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
const int INF =1100000000;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
int main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    while(cnt<(n*m)&&tl<=35)
    {
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                        st[j][k]=ch;
                    if(st[j][k]!='.')
                        cnt++;
                }
            }

        }
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}