#include <bits/stdc++.h>   using namespace std;   int n, m, p, last[10];   struct node {     int x, y;   };   int s[10];   char a[1005][1005];   int vis[1005][1005], ans[10], can[10];   queue<node> que[10];   int d[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};   void bfs() {     for (int i = 1; i <= p; i++) {       while (!que[i].empty()) que[i].pop();     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (a[i][j] !=  .  && a[i][j] !=  # ) {           que[a[i][j] -  0 ].push({i, j});           vis[i][j] = a[i][j] -  0 ;           last[a[i][j] -  0 ]++;         }       }     }     bool flag = true;     while (flag) {       flag = false;       for (int idx = 1; idx <= p; idx++) {         for (int ss = 1; ss <= s[idx] && can[idx] == 0; ss++) {           int num = last[idx];           last[idx] = 0;           for (int t = 1; t <= num; t++) {             node now = que[idx].front();             que[idx].pop();             ans[idx]++;             for (int i = 0; i < 4; i++) {               int xx = now.x + d[i][0], yy = now.y + d[i][1];               if (xx >= 0 && xx < n && yy >= 0 && yy < m && vis[xx][yy] == 0 &&                   a[xx][yy] ==  . ) {                 que[idx].push({xx, yy});                 vis[xx][yy] = idx;                 last[idx]++;               }             }           }           if (que[idx].empty()) can[idx] = idx;         }       }       for (int i = 1; i <= p; i++)         if (!que[i].empty()) flag = true;     }     for (int i = 1; i <= p; i++) printf( %d  , ans[i]);     printf(  n );   }   int main() {     while (scanf( %d %d %d , &n, &m, &p) != EOF) {       memset(a, 0, sizeof(a));       memset(s, 0, sizeof(s));       memset(ans, 0, sizeof(ans));       memset(vis, 0, sizeof(vis));       memset(last, 0, sizeof(last));       memset(can, 0, sizeof(can));       for (int i = 1; i <= p; i++) scanf( %d , &s[i]);       getchar();       for (int i = 0; i < n; i++) scanf( %s , a[i]);       bfs();     }     return 0;   }   