#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 100010
int a[N];
int main(){
	int n,m,i,x,k;
	while(~scanf("%d%d",&n,&m)){
		for(i=1;i<=n;i++)scanf("%d",&a[i]);
		x=0;
		while(m--){
		scanf("%d",&k);
		switch(k){
		case 1:
			scanf("%d%d",&i,&k);
			a[i]=k-x;
			break;
		case 2:
			scanf("%d",&k);
			x+=k;
			break;
		case 3:
			scanf("%d",&k);
			printf("%d\n",a[k]+x);
			break;
		}
		}
	}
return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii,pii> ppi;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<vii> vvii;
typedef vector<int, int> vipi;
typedef pair<pii,double> pid;
typedef pair<pii,int> piii;
typedef pair<double,double> pdd;
typedef pair<pdd,pdd> pddpdd;
typedef pair<pdd,double> pddd;
typedef pair<pddpdd,double> ppddpddd;
typedef pair<pii, pdd > ppid;
const static int INF = 1000000000;

int n,m,arr[100002],q,a,b;

int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1 ; i <= n; i++)
        scanf("%d",&arr[i]);
    for(int i = 0 ; i < m; i++){
        scanf("%d",&q);
        if(q==1){
            scanf("%d%d",&a,&b);
            arr[a] = b;
        }else if(q==2){
            scanf("%d",&a);
            for(int j = 1 ; j <= n; j++) arr[j]+=a;
        }else if(q==3){
            scanf("%d",&a);
            printf("%d\n",arr[a]);
        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
#include <map>
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii,pii> ppi;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<vii> vvii;
typedef vector<int, int> vipi;
typedef pair<pii,double> pid;
typedef pair<pii,int> piii;
typedef pair<double,double> pdd;
typedef pair<pdd,pdd> pddpdd;
typedef pair<pdd,double> pddd;
typedef pair<pddpdd,double> ppddpddd;
typedef pair<pii, pdd > ppid;
const static int INF = 1000000000;

int n,m,arr[100002],q,a,b,kurang=0;

int main(){
	scanf("%d%d",&n,&m);
	for(int i = 1 ; i <= n; i++)
		scanf("%d",&arr[i]);
	for(int i = 0 ; i < m; i++){
		scanf("%d",&q);
		if(q==1){
			scanf("%d%d",&a,&b);
			arr[a] = b-kurang;
		}else if(q==2){
			scanf("%d",&a);
			kurang += a;
		}else if(q==3){
			scanf("%d",&a);
			printf("%d\n",arr[a]+kurang);
		}
	}
    return 0;
}


#include <iostream>
using namespace std;
#define maxn 100005
int n,m,a[maxn];
int main()
{
    int i,t,v,x,y;
    cin>>n>>m;
    a[0]=0;
    for(i=1;i<=n;i++)
        cin>>a[i];
    while(m--)
    {
        cin>>t;
        if(t==1)
        {
            cin>>v>>x;
            a[v]=x;
        }
        if(t==2)
        {
            cin>>y;
            a[0]+=y;
        }
        if(t==3)
        {
            cin>>v;
            cout<<a[v]+a[0]<<endl;
        }
    }
    return 0;
}


#include <iostream>
using namespace std;
#define maxn 100005
int n,m,a[maxn];
int main()
{
    int i,t,v,x,y;
    cin>>n>>m;
    a[0]=0;
    for(i=1;i<=n;i++)
        cin>>a[i];
    while(m--)
    {
        cin>>t;
        if(t==1)
        {
            cin>>v>>x;
            a[v]=x-a[0];
        }
        if(t==2)
        {
            cin>>y;
            a[0]+=y;
        }
        if(t==3)
        {
            cin>>v;
            cout<<a[v]+a[0]<<endl;
        }
    }
    return 0;
}


//http://codeforces.com/problemset/problem/315/B

#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <cstdio>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <list>

using namespace std;

#define pii pair <int, int>
#define vvi vector <int>
#define ll long long

int size_n, size_m;
vector <int> el(0), whenChanged(0), sum(0);

int main()
{
	int ii;
	int tmp1, tmp2, tmp3, sumChanges = 0;

	cin >> size_n >> size_m;

	el.resize(size_n + 1);
	whenChanged.resize(size_n + 1);
	sum.resize(size_n + 1);

	for (ii = 1; ii <= size_n; ii++)
		cin >> el[ii];

	for (ii = 0; ii < size_m; ii++)
	{
		cin >> tmp1 >> tmp2;

		if (tmp1 == 1)
		{
			cin >> tmp3;

			whenChanged[tmp2] = sumChanges;
			el[tmp2] = tmp3;
		}
		else if (tmp1 == 2)
		{
			sum[sumChanges + 1] = sum[sumChanges] + tmp2;
			sumChanges++;
		}
		else
			cout << (el[tmp2] + (sum[sumChanges] - sum[whenChanged[tmp2]])) << endl;
	}

	return 0;
}


//http://codeforces.com/problemset/problem/315/B

#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <cstdio>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <list>

using namespace std;

#define pii pair <int, int>
#define vvi vector <int>
#define ll long long

int size_n, size_m;
vector <ll> el(0), whenChanged(0), sum(0);

int main()
{
	int ii;
	ll tmp1, tmp2, tmp3, sumChanges = 0;

	cin >> size_n >> size_m;

	el.resize(size_n + 1);
	whenChanged.resize(size_n + 1);
	sum.resize(size_n + 1);

	for (ii = 1; ii <= size_n; ii++)
		cin >> el[ii];

	for (ii = 0; ii < size_m; ii++)
	{
		cin >> tmp1 >> tmp2;

		if (tmp1 == 1)
		{
			cin >> tmp3;

			whenChanged[tmp2] = sumChanges;
			el[tmp2] = tmp3;
		}
		else if (tmp1 == 2)
		{
			sum[sumChanges + 1] = sum[sumChanges] + tmp2;
			sumChanges++;
		}
		else
			cout << (el[tmp2] + (sum[sumChanges] - sum[whenChanged[tmp2]])) << endl;
	}

	return 0;
}


//http://codeforces.com/problemset/problem/315/B

#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <cstdio>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <list>

using namespace std;

#define pii pair <int, int>
#define vvi vector <int>
#define ll long long

int size_n, size_m;
vector <ll> el(0), whenChanged(0), sum(0);

int main()
{
	int ii;
	ll tmp1, tmp2, tmp3, sumChanges = 0;

	cin >> size_n >> size_m;

	el.resize(size_n + 1);
	whenChanged.resize(size_n + 1);
	sum.resize(size_m + 1);

	for (ii = 1; ii <= size_n; ii++)
		cin >> el[ii];

	for (ii = 0; ii < size_m; ii++)
	{
		cin >> tmp1 >> tmp2;

		if (tmp1 == 1)
		{
			cin >> tmp3;

			whenChanged[tmp2] = sumChanges;
			el[tmp2] = tmp3;
		}
		else if (tmp1 == 2)
		{
			sum[sumChanges + 1] = sum[sumChanges] + tmp2;
			sumChanges++;
		}
		else
			cout << (el[tmp2] + (sum[sumChanges] - sum[whenChanged[tmp2]])) << endl;
	}

	return 0;
}


//#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <cmath>
#include <list>
#include <iomanip>
#include <set>
#include <map>
using namespace std;

typedef  long long LL;
typedef  unsigned long long ULL;
typedef  long double LD;
typedef  pair<int,int> PII;

#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define RFOR(i,a,b) for(int (i)=(a)-1;(i)>=(b);--(i))
#define MP make_pair
#define MOD 1000000007
#define INF 1000000000
#define PB push_back
//#define X first
//#define Y second
int n,m;
int a[100009];
LL s;
int main()
{
	//ios_base::sync_with_stdio(0);
	freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	cin>>n>>m;
	FOR(i,1,n+1)
		scanf("%d",&a[i]);
	int v,x,y;
	FOR(i,0,m)
	{
		int t;
		scanf("%d",&t);
		if(t==1)
		{
			scanf("%d%d",&v,&x);
			a[v]=x;
			continue;
		}
		if(t==2)
		{
			scanf("%d",&y);
			s+=y;
			continue;
		}
		scanf("%d",&y);
		printf("%I64d\n",a[y]+s);
	}
	return 0;
}


//#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <cmath>
#include <list>
#include <iomanip>
#include <set>
#include <map>
using namespace std;

typedef  long long LL;
typedef  unsigned long long ULL;
typedef  long double LD;
typedef  pair<int,int> PII;

#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define RFOR(i,a,b) for(int (i)=(a)-1;(i)>=(b);--(i))
#define MP make_pair
#define MOD 1000000007
#define INF 1000000000
#define PB push_back
//#define X first
//#define Y second
int n,m;
int a[100009];
LL s;
int main()
{
	//ios_base::sync_with_stdio(0);
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	cin>>n>>m;
	FOR(i,1,n+1)
		scanf("%d",&a[i]);
	int v,x,y;
	FOR(i,0,m)
	{
		int t;
		scanf("%d",&t);
		if(t==1)
		{
			scanf("%d%d",&v,&x);
			a[v]=x-s;
			continue;
		}
		if(t==2)
		{
			scanf("%d",&y);
			s+=y;
			continue;
		}
		scanf("%d",&y);
		printf("%I64d\n",a[y]+s);
	}
	return 0;
}


//#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <cmath>
#include <list>
#include <iomanip>
#include <set>
#include <map>
using namespace std;

typedef  long long LL;
typedef  unsigned long long ULL;
typedef  long double LD;
typedef  pair<int,int> PII;

#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define RFOR(i,a,b) for(int (i)=(a)-1;(i)>=(b);--(i))
#define MP make_pair
#define MOD 1000000007
#define INF 1000000000
#define PB push_back
//#define X first
//#define Y second
int n,m;
int a[100009];
LL s;
int main()
{
	//ios_base::sync_with_stdio(0);
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	cin>>n>>m;
	FOR(i,1,n+1)
		scanf("%d",&a[i]);
	int v,x,y;
	FOR(i,0,m)
	{
		int t;
		scanf("%d",&t);
		if(t==1)
		{
			scanf("%d%d",&v,&x);
			a[v]=x;
			continue;
		}
		if(t==2)
		{
			scanf("%d",&y);
			s+=y;
			continue;
		}
		scanf("%d",&y);
		printf("%I64d\n",a[y]+s);
	}
	return 0;
}


//#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <cmath>
#include <list>
#include <iomanip>
#include <set>
#include <map>
using namespace std;

typedef  long long LL;
typedef  unsigned long long ULL;
typedef  long double LD;
typedef  pair<int,int> PII;

#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define RFOR(i,a,b) for(int (i)=(a)-1;(i)>=(b);--(i))
#define MP make_pair
#define MOD 1000000007
#define INF 1000000000
#define PB push_back
//#define X first
//#define Y second
int n,m;
int a[100009];
LL s;
int main()
{
	//ios_base::sync_with_stdio(0);
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	cin>>n>>m;
	FOR(i,1,n+1)
		scanf("%d",&a[i]);
	int v,x,y;
	FOR(i,0,m)
	{
		int t;
		scanf("%d",&t);
		if(t==1)
		{
			scanf("%d%d",&v,&x);
			a[v]=x;
			continue;
		}
		if(t==2)
		{
			scanf("%d",&y);
			s+=y;
			continue;
		}
		scanf("%d",&y);
		printf("%d\n",a[y]+(int)s);
	}
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
    long int n,mas[100001],m,a,b,c;
    cin>>n>>m;
    for(int i = 0; i<n;i++)
    {
        cin>>mas[i];
    }
    for(int i = 0; i<m;i++)
    {
        cin>>a;
        if(a==3)
        {
            cin>>b;
            cout<<mas[b-1]<<endl;
        }
        if(a==2)
        {
            cin>>b;
            for(int i = 0; i<n;i++)
            {
                mas[i]+=b;
            }
        }
        if(a==1)
        {
            cin>>b>>c;
            mas[b-1]=c;
        }
    }
    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<cmath>
using namespace std;

int n, m, k, ans, sum, x, y;
bool l;
bool a[1005], b[1005];
string s, s1, s2;
vector<int> v, g;

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif

	cin >> n >> m;
	for(int i = 0; i < n; i++)
		cin >> k, v.push_back(k);
	for(int i = 0; i < m; i++)
	{
		cin >> k;
		if(k == 1)
		{
			cin >> x >> y;
			v[x-1] = y;
		}
		else if(k == 2)
		{
			cin >> x;
			ans += x;
		}
		else
		{
			cin >> x;
			cout << v[x-1] + ans << endl;
		}
	}

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<cmath>
using namespace std;

__int64 n, m, k, ans, sum, x, y, new_val;
bool l;
string s, s1, s2;
vector<int> v, g;
__int64 t[1000000], a[1000000];
void build(__int64 v, int tl, int tr)
{
    if(tl == tr)
        t[v] = a[tl];
    else
    {
        __int64 tm = (tl + tr) / 2;
        build(v*2, tl, tm);
        build(v*2 + 1, tm + 1, tr);
    }
}
void push(__int64 v)
{
    t[v*2] += t[v];
    t[v*2 + 1] += t[v];
    t[v] = 0;
}
void update(int v, int tl, int tr, int l, int r)
{
    if(tl == l && tr == r)
        t[v] += new_val;
}
void get(int v, int tl, int tr, int l, int r)
{
    if(l > r)
        return;
    if(tl == l && tr == r)
    {
        ans = t[v];
        return;
    }
    else
    {
        push(v);
        int tm = (tl + tr) / 2;
        get(v*2, tl, tm, l, min(tm, r));
        get(v*2 + 1, tm + 1, tr, max(tm + 1, l), r);
    }
}
void get1(int v, int tl, int tr, int l, int r)
{
    if(l > r)
        return;
    if(tl == l && tr == r)
        t[v] = y;
    else
    {
        push(v);
        int tm = (tl + tr) / 2;
        get1(v*2, tl, tm, l, min(tm, r));
        get1(v*2 + 1, tm + 1, tr, max(tm + 1, l), r);
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    build(1, 0, n - 1);
    for(int i = 0; i < m; i++)
    {
        cin >> k;
        if(k == 1)
        {
            cin >> x >> y;
            get1(1, 0, n - 1, x - 1, x - 1);
        }
        else if(k == 2)
        {
            cin >> new_val;
            update(1, 0, n - 1, 0, n - 1);
        }
        else
        {
            cin >> x;
            get(1, 0, n - 1, x - 1, x - 1);
            cout << ans << endl;
        }
    }

    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<cmath>
using namespace std;

__int64 n, m, k, ans, sum, x, y, new_val;
bool l;
string s, s1, s2;
vector<int> v, g;
__int64 t[100500], a[100500];
void build(__int64 v, int tl, int tr)
{
    if(tl == tr)
        t[v] = a[tl];
    else
    {
        __int64 tm = (tl + tr) / 2;
        build(v*2, tl, tm);
        build(v*2 + 1, tm + 1, tr);
    }
}
void push(__int64 v)
{
    t[v*2] += t[v];
    t[v*2 + 1] += t[v];
    t[v] = 0;
}
void update(int v, int tl, int tr, int l, int r)
{
    if(tl == l && tr == r)
        t[v] += new_val;
}
void get(int v, int tl, int tr, int l, int r)
{
    if(l > r)
        return;
    if(tl == l && tr == r)
    {
        ans = t[v];
        return;
    }
    else
    {
        push(v);
        int tm = (tl + tr) / 2;
        get(v*2, tl, tm, l, min(tm, r));
        get(v*2 + 1, tm + 1, tr, max(tm + 1, l), r);
    }
}
void get1(int v, int tl, int tr, int l, int r)
{
    if(l > r)
        return;
    if(tl == l && tr == r)
        t[v] = y;
    else
    {
        push(v);
        int tm = (tl + tr) / 2;
        get1(v*2, tl, tm, l, min(tm, r));
        get1(v*2 + 1, tm + 1, tr, max(tm + 1, l), r);
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    build(1, 0, n - 1);
    for(int i = 0; i < m; i++)
    {
        cin >> k;
        if(k == 1)
        {
            cin >> x >> y;
            get1(1, 0, n - 1, x - 1, x - 1);
        }
        else if(k == 2)
        {
            cin >> new_val;
            update(1, 0, n - 1, 0, n - 1);
        }
        else
        {
            cin >> x;
            get(1, 0, n - 1, x - 1, x - 1);
            cout << ans << endl;
        }
    }

    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<cmath>
using namespace std;

int n, m, k, ans, sum, x, y, new_val;
bool l;
string s, s1, s2;
vector<int> v, g;
int t[100500], a[100500];
void build(int v, int tl, int tr)
{
	if(tl == tr)
		t[v] = a[tl];
	else
	{
		int tm = (tl + tr) / 2;
		build(v*2, tl, tm);
		build(v*2 + 1, tm + 1, tr);
	}
}
void push(int v)
{
	t[v*2] += t[v];
	t[v*2 + 1] += t[v];
	t[v] = 0;
}
void update(int v, int tl, int tr, int l, int r)
{
	if(tl == l && tr == r)
		t[v] += new_val;
}
void get(int v, int tl, int tr, int l, int r)
{
	if(l > r)
		return;
	if(tl == l && tr == r)
	{
		ans = t[v];
		return;
	}
	else
	{
		push(v);
		int tm = (tl + tr) / 2;
		get(v*2, tl, tm, l, min(tm, r));
		get(v*2 + 1, tm + 1, tr, max(tm + 1, l), r);
	}
}
void get1(int v, int tl, int tr, int l, int r)
{
	if(l > r)
		return;
	if(tl == l && tr == r)
		t[v] = y;
	else
	{
		push(v);
		int tm = (tl + tr) / 2;
		get1(v*2, tl, tm, l, min(tm, r));
		get1(v*2 + 1, tm + 1, tr, max(tm + 1, l), r);
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif

	cin >> n >> m;
	for(int i = 0; i < n; i++)
		cin >> a[i];
	build(1, 0, n - 1);
	for(int i = 0; i < m; i++)
	{
		cin >> k;
		if(k == 1)
		{
			cin >> x >> y;
			get1(1, 0, n - 1, x - 1, x - 1);
		}
		else if(k == 2)
		{
			cin >> new_val;
			update(1, 0, n - 1, 0, n - 1);
		}
		else
		{
			cin >> x;
			get(1, 0, n - 1, x - 1, x - 1);
			cout << ans << endl;
		}
	}

	return 0;
}


#include"iostream"
#include<math.h>
long long a[100007],h[100007];
void f1(int x,int y)
{
  a[x]=y;
}
void f2(int n,int y)
{
	for(int i=1;i<=n;i++)
		a[i]+=y;
}
using namespace std;
int main()
{
	int m,n,u=0,z,t;long long x,y;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=m;i++)
		{cin>>t;
	if(t==1)
	{cin>>x>>y; f1(x,y);}
	else if(t==2)
	{cin>>y; f2(n,y);}
	else
	{cin>>z; h[u++]=a[z];}
	}
	for(int i=0;i<u;i++)
		cout<<h[i]<<'\n';
	cin>>n;
	return 0;
}


#include"iostream"
#include<math.h>
long long a[100007],h[100007],sum;
void f1(int x,int y,long long sum)
{
  a[x]=y-sum;
}
using namespace std;
int main()
{
	int m,n,u=0,z,t;long long x,y;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=m;i++)
		{cin>>t;
	if(t==1)
	{cin>>x>>y; f1(x,y,sum);}
	else if(t==2)
	{cin>>y; sum+=y;}
	else
	{cin>>z; h[u++]=a[z]+sum;}
	}
	for(int i=0;i<u;i++)
		cout<<h[i]<<'\n';
	cin>>n;
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <map>
#include <cmath>
#include <set>
#include <cstdlib>
using namespace std;
typedef long long ll;

ll inc[10010];
int ch[10010];
int n, m;

int main()
{
    #if !defined(ONLINE_JUDGE)
    freopen("input.txt", "r", stdin);
    freopen("output.txt ", "w", stdout);
    #endif

	memset(inc, 0, sizeof(inc));

	cin >> n >> m;
	for (int i = 1; i <= n; ++i) {
		cin >> ch[i];
	}

	ll sum = 0;
	for (int i = 0; i < m; ++i) {
		int t;
		cin >> t;
		if (t == 1) {
			int v, x;
			cin >> v >> x;
			ch[v] = x;
			inc[v] = sum;
		} else
		if (t == 2) {
			int y;
			cin >> y;
			sum += y;
		} else {
			int q;
			cin >> q;
			cout << ch[q] + sum - inc[q] << endl;
		}
	}

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <map>
#include <cmath>
#include <set>
#include <cstdlib>
using namespace std;
typedef long long ll;

ll inc[100100];
ll ch[100100];
ll n, m;

int main()
{
    #if !defined(ONLINE_JUDGE)
    freopen("input.txt", "r", stdin);
    freopen("output.txt ", "w", stdout);
    #endif

	memset(inc, 0, sizeof(inc));

	cin >> n >> m;
	for (int i = 1; i <= n; ++i) {
		cin >> ch[i];
	}

	ll sum = 0;
	for (int i = 0; i < m; ++i) {
		int t;
		cin >> t;
		if (t == 1) {
			int v, x;
			cin >> v >> x;
			ch[v] = x;
			inc[v] = sum;
		} else
		if (t == 2) {
			int y;
			cin >> y;
			sum += y;
		} else {
			int q;
			cin >> q;
			cout << ch[q] + sum - inc[q] << endl;
		}
	}

    return 0;
}


#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

long long int m[100000],res[100000],p;
int main()
{

int n,l=0,q;

cin>>n>>q;

for(int i=0;i<n;++i)
cin>>m[i];

for(int i=0;i<q;++i)
{
	int t;
	cin>>t;
	if(t==1){int v,x;cin>>v>>x;m[v-1]=x-p;}
	if(t==2){int y;cin>>y;p+=y;}
	if(t==3){int u;cin>>u;res[l]=p+m[u-1];++l;}
}

for(int i=0;i<l;++i)
cout<<res[i]<<endl;

}


#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

long long int m[100000],res[100000],p;
int main()
{

int n,l=0,q;

cin>>n>>q;

for(int i=0;i<n;++i)
cin>>m[i];

for(int i=0;i<q;++i)
{
	int t;
	cin>>t;
	if(t==1){int v,x;cin>>v>>x;m[v-1]=x;}
	if(t==2){int y;cin>>y;p+=y;}
	if(t==3){int u;cin>>u;res[l]=p+m[u-1];++l;}
}

for(int i=0;i<l;++i)
cout<<res[i]<<endl;

}


#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;
void main()
{
	int m, n, y,operation,sum=0;
	long long v, x;
	cin >> n >> m;
	vector<int> vec,res;
	for (int i = 0; i < n; i++)
	{
		cin >> y;
		vec.push_back(y);
	}

	while (m--)
	{
		cin >> operation;
		if (operation == 1)
		{
			cin >> v >> x;
			vec[v - 1] = x;
		}
		else if (operation == 2)
		{
			cin >> y;
			for (int i = 0; i < n; i++)
			{
				vec[i] += y;
			}
		}
		else
		{
			cin >> y;
			cout << vec[y - 1] << endl;
		}
	}

}


#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;
void main()
{
	int m, n, y,operation,sum=0;
	long long v, x;
	cin >> n >> m;
	vector<int> vec,res;
	for (int i = 0; i < n; i++)
	{
		cin >> y;
		vec.push_back(y);
	}

	while (m--)
	{
		cin >> operation;
		if (operation == 1)
		{
			cin >> v >> x;
			vec[v - 1] = x-sum;
		}
		else if (operation == 2)
		{
			cin >> y;
			sum += y;
		}
		else
		{
			cin >> y;
			cout << vec[y - 1]+sum << endl;
		}
	}

}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cctype>

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long

int main(){
    //freopen("input.in", "r", stdin);
    //freopen("input.out", "w", stdout);

    int n, m;
    cin >> n >> m;

    int a[10005];

    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }

    int plus = 0;
    for (int i = 1; i <= m; i++){
        int t;
        cin >> t;

        if (t == 1){
            int v, x;
            cin >> v >> x;
            a[v] = x;
        }
        else if (t == 2){
            int y;
            cin >> y;
            plus += y;
        }
        else{
            int q;
            cin >> q;
            cout << a[q] + plus << endl;
        }
    }

    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cctype>

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long

int main(){
    //freopen("input.in", "r", stdin);
    //freopen("input.out", "w", stdout);

    int n, m;
    cin >> n >> m;

    vector<int> a;

    for (int i = 1; i <= n; i++){
        int t;
        cin >> t;
        a.push_back(t);
    }

    int plus = 0;
    for (int i = 1; i <= m; i++){
        int t;
        cin >> t;

        if (t == 1){
            int v, x;
            cin >> v >> x;
            a[v-1] = x;
        }
        else if (t == 2){
            int y;
            cin >> y;
            plus += y;
        }
        else{
            int q;
            cin >> q;
            q--;
            cout << a[q] + plus << endl;
        }
    }

    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cctype>

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long

int main(){
    //freopen("input.in", "r", stdin);
    //freopen("input.out", "w", stdout);

    int n, m;
    cin >> n >> m;

    ull a[100005];

    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }

    int plus = 0;
    for (int i = 1; i <= m; i++){
        int t;
        cin >> t;

        if (t == 1){
            int v, x;
            cin >> v >> x;
            a[v] = x - plus;
        }
        else if (t == 2){
            int y;
            cin >> y;
            plus += y;
        }
        else{
            int q;
            cin >> q;
            cout << a[q] + plus << endl;
        }
    }

    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cctype>

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long

int main(){
    //freopen("input.in", "r", stdin);
    //freopen("input.out", "w", stdout);

    int n, m;
    cin >> n >> m;

    int a[10005];

    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }

    int plus = 0;
    for (int i = 1; i <= m; i++){
        int t;
        cin >> t;

        if (t == 1){
            int v, x;
            cin >> v >> x;
            a[v] = x - plus;
        }
        else if (t == 2){
            int y;
            cin >> y;
            plus += y;
        }
        else{
            int q;
            cin >> q;
            cout << a[q] + plus << endl;
        }
    }

    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cctype>

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long

int main(){
    //freopen("input.in", "r", stdin);
    //freopen("input.out", "w", stdout);

    int n, m;
    cin >> n >> m;

    ull a[10005];

    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }

    int plus = 0;
    for (int i = 1; i <= m; i++){
        int t;
        cin >> t;

        if (t == 1){
            int v, x;
            cin >> v >> x;
            a[v] = x - plus;
        }
        else if (t == 2){
            int y;
            cin >> y;
            plus += y;
        }
        else{
            int q;
            cin >> q;
            cout << a[q] + plus << endl;
        }
    }

    return 0;
}


#include<stdio.h>
#include<vector>
#include<iostream>
using namespace std;
int main()
{
	int n, m, l;
	scanf("%d%d",&n,&m);
	bool ff=true;
	vector <long long> v;
	long long *a=new long long[n];
	long long f=0, x;
	for(int i=0;i<n;i++)
	{
		//scanf("%d",&a[i]);
		cin>>a[i];
	}
	for(int i=0;i<m;i++)
	{
		scanf("%d",&l);
		if(l==1)
		{
			cin>>l>>x;//scanf("%d%d",&l,&x);
			a[l-1]=x;
		}
		else if(l==2)
		{
			cin>>x;
			f+=x;
		}
		else if(l==3)
		{
			cin>>x;
			v.push_back(f+a[x-1]);
			//cout<< f+a[x-1]<<'\n';

		}
	}
	for(int i=0;i<v.size()-1; i++)
	{
		cout<< v[i]<<'\n';
	}
	cout<< v[v.size()-1];
	return 0;
}


#include<stdio.h>
#include<vector>
#include<iostream>
using namespace std;
int main()
{
	int n, m, l;
	scanf("%d%d",&n,&m);
	bool ff=true;
	vector <long long> v;
	long long *a=new long long[n];
	long long f=0, x;
	for(int i=0;i<n;i++)
	{
		//scanf("%d",&a[i]);
		cin>>a[i];
	}
	for(int i=0;i<m;i++)
	{
		scanf("%d",&l);
		if(l==1)
		{
			cin>>l>>x;//scanf("%d%d",&l,&x);
			a[l-1]=x;
		}
		else if(l==2)
		{
			cin>>x;
			f+=x;
		}
		else if(l==3)
		{
			cin>>x;
			//v.push_back(f+a[x-1]);
			cout<< f+a[x-1]<<'\n';

		}
	}
	return 0;
}


#include<stdio.h>
//#include<vector>
#include<iostream>
using namespace std;
int main()
{
	int n, m, l;
	scanf("%d%d",&n,&m);
	bool ff=true;
	//vector <long long> v;
	long long *a=new long long[n];
	long long *b=new long long[n];
	long long f=0, x;
	for(int i=0;i<n;i++)
	{
		//scanf("%d",&a[i]);
		cin>>a[i];
		b[i]=0;
	}
	for(int i=0;i<m;i++)
	{
		scanf("%d",&l);
		if(l==1)
		{
			cin>>l>>x;//scanf("%d%d",&l,&x);
			a[l-1]=x;
			b[l-1]=f;
		}
		else if(l==2)
		{
			cin>>x;
			f+=x;
		}
		else if(l==3)
		{
			cin>>x;
			//v.push_back(f+a[x-1]);
			cout<< f+a[x-1]-b[x-1]<<'\n';

		}
	}
	//for(long long i=0;i<v.size(); i++)
	//{
	//	cout<< v[i]<<'\n';
	//}
	return 0;
}


#include<stdio.h>
int main()
{
	int n, m, l;
	scanf("%d%d",&n,&m);
	long long *a=new long long[n];
	long long f=0, x;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(int i=0;i<m;i++)
	{
		scanf("%d",&l);
		if(l==1)
		{
			scanf("%d%d",&l,&x);
			a[l-1]=x;
		}
		else if(l==2)
		{
			scanf("%d",&x);
			f+=x;
		}
		else if(l==3)
		{
			scanf("%d",&x);
			printf("%d\n", f+a[x-1]);
		}
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <cmath>
#include <vector>
#include <set>
#include <string>
#include <string.h>
#include <map>
#include <queue>
#include <algorithm>

using namespace std;

#define lli long long int

const int N = 100100;

int n, m;
lli a[N];
lli times[N];

lli add[N];

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	cin >> n >> m;
	for(int i = 0; i < n; ++i) {
		cin >> a[i];
		times[i] = -1;
	}

	int time = -1;
	for(int q = 0; q < m; ++q) {
		int t, pos;
		cin >> t >> pos;
		if (t == 1) {
			int x;
			cin >> x;
			--pos;
			a[pos] = x;
			times[pos] = time;
		} else if (t==2) {
			++time;
			add[time] = pos;
			if (time > 0) add[time] += add[time-1];
		} else {
			--pos;
			lli ans = a[pos];
			if (times[pos] < time) {
				ans += add[time];
				if (times[pos] != -1) {
					ans -= add[times[pos]];
				}
			}
			cout << ans << endl;
		}
	}

	return 0;
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <cmath>
#include <vector>
#include <set>
#include <string>
#include <string.h>
#include <map>
#include <queue>
#include <algorithm>

using namespace std;

#define lli long long int

const int N = 10010;

int n, m;
lli a[N];
lli times[N];

lli add[N];
lli p_add[N];

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	cin >> n >> m;
	for(int i = 0; i < n; ++i) {
		cin >> a[i];
		times[i] = -1;
	}

	int time = -1;
	for(int q = 0; q < m; ++q) {
		int t, pos;
		cin >> t >> pos;
		if (t == 1) {
			int x;
			cin >> x;
			--pos;
			a[pos] = x;
			times[pos] = time;
		} else if (t==2) {
			++time;
			add[time] = pos;
			if (time > 0) add[time] += add[time-1];
		} else {
			--pos;
			lli ans = a[pos];
			if (times[pos] < time) {
				ans += add[time];
				if (times[pos] != -1) {
					ans -= add[times[pos]];
				}
			}
			cout << ans << endl;
		}
	}

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int a[100100];

int main()
{
	int n,m,t,v,x,y,q;
	cin>>n>>m;
	for(int i=0;i<n;i++)
		cin>>a[i];
	for(int i=0;i<m;i++)
	{
		cin>>t;
		if(t==1)
		{
			cin>>v>>x;
			a[v-1]=x;
		}
		else if(t==2)
		{
			cin>>y;
			for(int i=0;i<n;i++)
				a[i]+=y;
		}
		else
		{
			cin>>q;
			cout<<a[q-1]<<endl;
		}
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int a[100100];

int main()
{
	int n,m,t,v,x,y,q,sum=0;
	cin>>n>>m;
	for(int i=0;i<n;i++)
		cin>>a[i];
	for(int i=0;i<m;i++)
	{
		cin>>t;
		if(t==1)
		{
			cin>>v>>x;
			a[v-1]=x-sum;
		}
		else if(t==2)
		{
			cin>>y;
			sum+=y;
		}
		else
		{
			cin>>q;
			cout<<a[q-1]+sum<<endl;
		}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define LL long long int
#define maxn 100008
#define lson 2*id,l,mid
#define rson 2*id+1,mid+1,r
LL key[maxn];
struct ST
{
    int l,r;
    LL add,len;
}st[4*maxn];
void buildtree(int id,int l,int r)
{
    st[id].l = l;
    st[id].r = r;
    if(l == r)
    {
        st[id].len = key[l];
        st[id].add = 0;
        return;
    }
    int mid = ( l + r ) >> 1;
    buildtree(lson);
    buildtree(rson);
}
void PushDown(int id)
{
    if(st[id].add)
    {
        st[2*id].add += st[id].add;
        st[2*id+1].add += st[id].add;
        st[id].add = 0;
    }
}
void udset(int id,int l,int r,int pos,LL nkey)
{
    if(st[id].l == st[id].r)
    {
        st[id].len = nkey;
        st[id].add = 0;
        return;
    }
    PushDown(id);
    if(st[2*id].r >= pos)
    {
        udset(2*id,l,r,pos,nkey);
        return;
    }
    if(st[2*id+1].l <= pos)
    {
        udset(2*id+1,l,r,pos,nkey);
        return;
    }
}
void udadd(int id,int l,int r,LL nkey)
{
    if(st[id].l == l && st[id].r == r)
    {
        st[id].add += nkey;
        return;
    }
    PushDown(id);
    if(st[2*id].r >= r)
    {
        udadd(2*id,l,r,nkey);
        return;
    }
    if(st[2*id+1].l <= l)
    {
        udadd(2*id+1,l,r,nkey);
        return;
    }
    udadd(2*id,l,st[2*id].r,nkey);
    udadd(2*id+1,st[2*id+1].l,r,nkey);
}
LL query(int id,int l,int r,int pos)
{
    if(st[id].l == st[id].r)
    {
        return st[id].len + st[id].add;
    }
    PushDown(id);
    if(st[2*id].r >= pos)
    {
        return query(2*id,l,r,pos);
    }
    if(st[2*id+1].l <= pos)
    {
        return query(2*id+1,l,r,pos);
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&key[i]);
        }
        buildtree(1,1,n);
        for(int i=1;i<=m;i++)
        {
            int ope;
            scanf("%d",&ope);
            if(ope == 2)
            {
                LL addd;
                scanf("%I64d",&addd);
                udadd(1,1,n,addd);
            }
            else if(ope == 1)
            {
                int v;  LL num;
                scanf("%d%I64d",&v,&num);
                udset(1,1,n,v,num);
            }
            else
            {
                int v;
                scanf("%d",&v);
                printf("%I64d\n",query(1,1,n,v));
            }
        }
        return 0;
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define LL long long int
#define maxn 100008
#define lson 2*id,l,mid
#define rson 2*id+1,mid+1,r
LL key[maxn];
struct ST
{
    int l,r;
    LL add,len;
}st[4*maxn];
void buildtree(int id,int l,int r)
{
    st[id].l = l;
    st[id].r = r;
    if(l == r)
    {
        st[id].len = key[l];
        st[id].add = 0;
        return;
    }
    int mid = ( l + r ) >> 1;
    buildtree(lson);
    buildtree(rson);
}
void PushDown(int id)
{
    if(st[id].add)
    {
        st[2*id].add += st[id].add;
        st[2*id+1].add += st[id].add;
        st[id].add = 0;
    }
}
void udset(int id,int l,int r,int pos,LL nkey)
{
    if(st[id].l == st[id].r)
    {
        st[id].len = nkey;
        st[id].add = 0;
        return;
    }
    PushDown(id);
    if(st[2*id].r >= pos)
    {
        udset(2*id,l,r,pos,nkey);
        return;
    }
    if(st[2*id+1].l <= pos)
    {
        udset(2*id+1,l,r,pos,nkey);
        return;
    }
}
void udadd(int id,int l,int r,LL nkey)
{
    if(st[id].l == l && st[id].r == r)
    {
        st[id].add += nkey;
        return;
    }
    PushDown(id);
    if(st[2*id].r >= r)
    {
        udadd(2*id,l,r,nkey);
        return;
    }
    if(st[2*id+1].l <= l)
    {
        udadd(2*id+1,l,r,nkey);
        return;
    }
    udadd(2*id,l,st[2*id].r,nkey);
    udadd(2*id+1,st[2*id+1].l,r,nkey);
}
LL query(int id,int l,int r,int pos)
{
    if(st[id].l == st[id].r)
    {
        return st[id].len + st[id].add;
    }
    PushDown(id);
    if(st[2*id].r >= pos)
    {
        return query(2*id,l,r,pos);
    }
    if(st[2*id+1].l <= pos)
    {
        return query(2*id+1,l,r,pos);
    }
}
int main()
{
    freopen("in.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&key[i]);
        }
        buildtree(1,1,n);
        for(int i=1;i<=m;i++)
        {
            int ope;
            scanf("%d",&ope);
            if(ope == 2)
            {
                LL addd;
                scanf("%I64d",&addd);
                udadd(1,1,n,addd);
            }
            else if(ope == 1)
            {
                int v;  LL num;
                scanf("%d%I64d",&v,&num);
                udset(1,1,n,v,num);
            }
            else
            {
                int v;
                scanf("%d",&v);
                printf("%I64d\n",query(1,1,n,v));
            }
        }
        return 0;
    }
}


#include<bits/stdc++.h>
using namespace std;
int n,m,p;
int a[15];
string s[1005];
int dx[5]={1,0,-1,0};
int dy[5]={0,-1,0,1};
int ans[15]={0};
bool home[1005][1005];
inline bool isval(int x,int y)
{
	if(x>=0 and x<n and y>=0 and y<m and s[x][y]=='.')
	{
		return true;
	}
	return false;
}
inline bool isfull()
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(s[i][j]=='.')
			{
				for(int k=0;k<4;k++)
				{
					int x1=i+dx[k];
					int y1=j+dy[k];
					if(x1>=0 and x1<n and y1>=0 and y1<m and s[x1][y1]!='#')
					{
						return false;
					}
				}
			}
		}
	}
	return true;
}
inline void dfs(int x,int y,int player,int turns)
{
	if(turns==0)
	{
		return;
	}
	for(int i=0;i<4;i++)
	{
		int x1=x+dx[i];
		int y1=y+dy[i];
		if(isval(x1,y1))
		{
			s[x1][y1]=char(player+'0');
			dfs(x1,y1,player,turns-1);
		}
	}
}
int main()
{
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<n;i++)
	{
		cin>>s[i];
	}
	while(!isfull())
	{
		for(int pl=1;pl<=p;pl++)
		{
			//cout<<pl<<endl;
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<m;j++)
				{
					home[i][j]=false;
				}
			}
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<m;j++)
				{
					if((s[i][j]-'0')==pl)
					{
						home[i][j]=true;
					}
				}
			}
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<m;j++)
				{
					if(home[i][j])
					{
						dfs(i,j,pl,a[pl]);
					}
				}
			}
			/*for(int i=0;i<n;i++)
			{
				cout<<s[i]<<endl;
			}*/
		}
	}
	for(int i=0;i<n;i++)
	{
	//	cout<<s[i]<<endl;
		for(int j=0;j<m;j++)
		{
			if(s[i][j]-'0'>=0 and s[i][j]-'0'<=9)
			{
				ans[s[i][j]-'0']++;
			}
		}
	}
	for(int i=1;i<=p;i++)
	{
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,p;
int a[15];
string s[1005];
int dx[5]={1,0,-1,0};
int dy[5]={0,-1,0,1};
int ans[15]={0};
queue<pair<int,int> > q;
bool vis[1005][1005];
bool check=true;
inline bool isval(int x,int y)
{
	if(x>=0 and x<n and y>=0 and y<m and s[x][y]=='.')
	{
		return true;
	}
	return false;
}
/*inline bool isfull()
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(s[i][j]=='.')
			{
				for(int k=0;k<4;k++)
				{
					int x1=i+dx[k];
					int y1=j+dy[k];
					if(x1>=0 and x1<n and y1>=0 and y1<m and s[x1][y1]!='#')
					{
						return false;
					}
				}
			}
		}
	}
	return true;
}
*/
inline void dfs(int x,int y,int player,int turns)
{
	if(turns==0)
	{
		return;
	}
	for(int i=0;i<4;i++)
	{
		int x1=x+dx[i];
		int y1=y+dy[i];
		if(isval(x1,y1))
		{
			s[x1][y1]=char(player+'0');
			check=true;
			vis[x][y]=true;
			dfs(x1,y1,player,turns-1);
		}
	}
}
int main()
{
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<n;i++)
	{
		cin>>s[i];
	}
	while(check)
	{
		check=false;
		for(int pl=1;pl<=p;pl++)
		{
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<m;j++)
				{
					if((s[i][j]-'0')==pl and !vis[i][j])
					{
						q.push(make_pair(i,j));
					}
				}
			}
			while(!q.empty())
			{
				int x=q.front().first;
				int y=q.front().second;
				q.pop();
				dfs(x,y,pl,a[pl]);
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		//cout<<s[i]<<endl;
		for(int j=0;j<m;j++)
		{
			if(s[i][j]-'0'>=1 and s[i][j]-'0'<=p)
			{
				ans[s[i][j]-'0']++;
			}
		}
	}
	for(int i=1;i<=p;i++)
	{
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct cells
{
	int x,y,turns;
};
vector<cells> start[15];
int n,m,p;
int a[15];
string s[1005];
int dx[5]={1,0,-1,0};
int dy[5]={0,-1,0,1};
int ans[15]={0};
queue<cells> q[15];
bool check=true;
inline bool isval(int x,int y)
{
	if(x>=0 and x<n and y>=0 and y<m and s[x][y]=='.')
	{
		return true;
	}
	return false;
}
inline void bfs(int x,int y,int player,int steps)
{
	if(steps==0)
	{
		return;
	}
	for(int i=0;i<4;i++)
	{
		int x1=x+dx[i];
		int y1=y+dy[i];
		if(isval(x1,y1))
		{
			if(!check)
			{
				check=true;
			}
			s[x1][y1]=char(player+'0');
			q[player].push((cells){x1,y1,steps-1});
		}
	}
}
int main()
{
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<n;i++)
	{
		cin>>s[i];
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(s[i][j]-'0'>=1 and s[i][j]-'0'<=p)
			{
				start[s[i][j]-'0'].push_back((cells){i,j,a[s[i][j]-'0']});
			}
		}
	}
	while(check)
	{
		check=false;
		for(int pl=1;pl<=p;pl++)
		{
			for(int i=0;i<start[pl].size();i++)
			{
				q[pl].push(start[pl][i]);
			}
			start[pl].clear();
			while(!q[pl].empty())
			{
				int x=q[pl].front().x;
				int y=q[pl].front().y;
				int steps=q[pl].front().turns;
				if(steps==0)
				{
					start[pl].push_back((cells){x,y,a[pl]});
				}
				q[pl].pop();
				bfs(x,y,pl,steps);
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if((s[i][j]-'0')>=1 and (s[i][j]-'0')<=p)
			{
				ans[s[i][j]-'0']++;
			}
		}
	}
	for(int i=1;i<=p;i++)
	{
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,p;
int a[15];
string s[1005];
int dx[5]={1,0,-1,0};
int dy[5]={0,-1,0,1};
int ans[15]={0};
inline bool isval(int x,int y)
{
	if(x>=0 and x<n and y>=0 and y<m and s[x][y]=='.')
	{
		return true;
	}
	return false;
}
inline bool isfull()
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(s[i][j]=='.')
			{
				for(int k=0;k<4;k++)
				{
					int x1=i+dx[k];
					int y1=j+dy[k];
					if(x1>=0 and x1<n and y1>=0 and y1<m and s[x1][y1]!='#')
					{
						return false;
					}
				}
			}
		}
	}
	return true;
}
inline void dfs(int x,int y,int player,int turns)
{
	if(turns==0)
	{
		return;
	}
	for(int i=0;i<4;i++)
	{
		int x1=x+dx[i];
		int y1=y+dy[i];
		if(isval(x1,y1))
		{
			s[x1][y1]=char(player+'0');
			dfs(x1,y1,player,turns-1);
		}
	}
}
int main()
{
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<n;i++)
	{
		cin>>s[i];
	}
	while(!isfull())
	{
		for(int pl=1;pl<=p;pl++)
		{
			//cout<<pl<<endl;
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<m;j++)
				{
					if((s[i][j]-'0')==pl)
					{
						dfs(i,j,pl,a[pl]);
						break;
					}
				}
			}
			/*for(int i=0;i<n;i++)
			{
				cout<<s[i]<<endl;
			}*/
		}
	}
	for(int i=0;i<n;i++)
	{
	//	cout<<s[i]<<endl;
		for(int j=0;j<m;j++)
		{
			if(s[i][j]-'0'>=0 and s[i][j]-'0'<=9)
			{
				ans[s[i][j]-'0']++;
			}
		}
	}
	for(int i=1;i<=p;i++)
	{
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,p;
int a[15];
string s[1005];
int dx[5]={1,0,-1,0};
int dy[5]={0,-1,0,1};
int ans[15]={0};
queue<pair<int,int> > q;
bool vis[1005][1005];
bool check=true;
inline bool isval(int x,int y)
{
	if(x>=0 and x<n and y>=0 and y<m and s[x][y]=='.')
	{
		return true;
	}
	return false;
}
inline void dfs(int x,int y,int player,int turns)
{
	if(turns==0)
	{
		return;
	}
	for(int i=0;i<4;i++)
	{
		int x1=x+dx[i];
		int y1=y+dy[i];
		if(isval(x1,y1))
		{
			s[x1][y1]=char(player+'0');
			if(!check)
			{
				check=true;
			}
			vis[x][y]=true;
			dfs(x1,y1,player,turns-1);
		}
	}
}
int main()
{
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<n;i++)
	{
		cin>>s[i];
	}
	while(check)
	{
		check=false;
		for(int pl=1;pl<=p;pl++)
		{
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<m;j++)
				{
					if((s[i][j]-'0')==pl and !vis[i][j])
					{
						q.push(make_pair(i,j));
					}
				}
			}
			while(!q.empty())
			{
				int x=q.front().first;
				int y=q.front().second;
				q.pop();
				dfs(x,y,pl,a[pl]);
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(s[i][j]-'0'>=1 and s[i][j]-'0'<=p)
			{
				ans[s[i][j]-'0']++;
			}
		}
	}
	for(int i=1;i<=p;i++)
	{
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,p;
int a[15];
string s[1005];
int dx[5]={1,0,-1,0};
int dy[5]={0,-1,0,1};
int ans[15]={0};
queue<pair<int,int> > q;
bool vis[1005][1005];
inline bool isval(int x,int y)
{
	if(x>=0 and x<n and y>=0 and y<m and s[x][y]=='.')
	{
		return true;
	}
	return false;
}
inline bool isfull()
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(s[i][j]=='.')
			{
				for(int k=0;k<4;k++)
				{
					int x1=i+dx[k];
					int y1=j+dy[k];
					if(x1>=0 and x1<n and y1>=0 and y1<m and s[x1][y1]!='#')
					{
						return false;
					}
				}
			}
		}
	}
	return true;
}
inline void dfs(int x,int y,int player,int turns)
{
	if(turns==0)
	{
		return;
	}
	for(int i=0;i<4;i++)
	{
		int x1=x+dx[i];
		int y1=y+dy[i];
		if(isval(x1,y1))
		{
			s[x1][y1]=char(player+'0');
			vis[x][y]=true;
			dfs(x1,y1,player,turns-1);
		}
	}
}
int main()
{
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<n;i++)
	{
		cin>>s[i];
	}
	while(!isfull())
	{
		for(int pl=1;pl<=p;pl++)
		{
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<m;j++)
				{
					if((s[i][j]-'0')==pl and !vis[i][j])
					{
						q.push(make_pair(i,j));
					}
				}
			}
			while(!q.empty())
			{
				int x=q.front().first;
				int y=q.front().second;
				q.pop();
				dfs(x,y,pl,a[pl]);
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		//cout<<s[i]<<endl;
		for(int j=0;j<m;j++)
		{
			if(s[i][j]-'0'>=1 and s[i][j]-'0'<=p)
			{
				ans[s[i][j]-'0']++;
			}
		}
	}
	for(int i=1;i<=p;i++)
	{
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,p;
int a[15];
string s[1005];
int dx[5]={1,0,-1,0};
int dy[5]={0,-1,0,1};
int ans[15]={0};
queue<pair<int,int> > q;
bool check=true;
inline bool isval(int x,int y)
{
	if(x>=0 and x<n and y>=0 and y<m and s[x][y]=='.')
	{
		return true;
	}
	return false;
}
inline void dfs(int x,int y,int player,int turns)
{
	if(turns==0)
	{
		return;
	}
	for(int i=0;i<4;i++)
	{
		int x1=x+dx[i];
		int y1=y+dy[i];
		if(isval(x1,y1))
		{
			s[x1][y1]=char(player+'0');
			if(!check)
			{
				check=true;
			}
			dfs(x1,y1,player,turns-1);
		}
	}
}
int main()
{
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<n;i++)
	{
		cin>>s[i];
	}
	while(check)
	{
		check=false;
		for(int pl=1;pl<=p;pl++)
		{
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<m;j++)
				{
					if((s[i][j]-'0')==pl)
					{
						q.push(make_pair(i,j));
					}
				}
			}
			while(!q.empty())
			{
				int x=q.front().first;
				int y=q.front().second;
				q.pop();
				dfs(x,y,pl,a[pl]);
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if((s[i][j]-'0')>=1 and (s[i][j]-'0')<=p)
			{
				ans[s[i][j]-'0']++;
			}
		}
	}
	for(int i=1;i<=p;i++)
	{
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,p;
int a[15];
string s[1005];
int dx[5]={1,0,-1,0};
int dy[5]={0,-1,0,1};
int ans[15]={0};
queue<pair<int,int> > q;
inline bool isval(int x,int y)
{
	if(x>=0 and x<n and y>=0 and y<m and s[x][y]=='.')
	{
		return true;
	}
	return false;
}
inline bool isfull()
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(s[i][j]=='.')
			{
				for(int k=0;k<4;k++)
				{
					int x1=i+dx[k];
					int y1=j+dy[k];
					if(x1>=0 and x1<n and y1>=0 and y1<m and s[x1][y1]!='#')
					{
						return false;
					}
				}
			}
		}
	}
	return true;
}
inline void dfs(int x,int y,int player,int turns)
{
	if(turns==0)
	{
		return;
	}
	for(int i=0;i<4;i++)
	{
		int x1=x+dx[i];
		int y1=y+dy[i];
		if(isval(x1,y1))
		{
			s[x1][y1]=char(player+'0');
			dfs(x1,y1,player,turns-1);
		}
	}
}
int main()
{
	cin>>n>>m>>p;
	for(int i=1;i<=p;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<n;i++)
	{
		cin>>s[i];
	}
	while(!isfull())
	{
		for(int pl=1;pl<=p;pl++)
		{
			//cout<<pl<<endl;
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<m;j++)
				{
					if((s[i][j]-'0')==pl)
					{
						q.push(make_pair(i,j));
					}
				}
			}
			while(!q.empty())
			{
				int x=q.front().first;
				int y=q.front().second;
				q.pop();
				dfs(x,y,pl,a[pl]);
			}
			/*for(int i=0;i<n;i++)
			{
				cout<<s[i]<<endl;
			}*/
		}
	}
	for(int i=0;i<n;i++)
	{
	//	cout<<s[i]<<endl;
		for(int j=0;j<m;j++)
		{
			if(s[i][j]-'0'>=0 and s[i][j]-'0'<=9)
			{
				ans[s[i][j]-'0']++;
			}
		}
	}
	for(int i=1;i<=p;i++)
	{
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int i,j,k,n,m,p,s[10],sk,rez[10];
char c[1003][1003];
list<pair<int,int> > active[10],x;
int main()
{
    //std::cin.sync_with_stdio(false);
    //std::cin.tie(0);
    cin >> n >> m >> p;
    i=0;
    while (i<p)
    {
        cin >> s[i];
        i++;
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            cin >> c[i][j];
            if (c[i][j]!='.')
            {
                sk++;
            }
            if (c[i][j]=='1')
            {
                active[0].push_back({i,j});
            }
            if (c[i][j]=='2')
            {
                active[1].push_back({i,j});
            }
            if (c[i][j]=='3')
            {
                active[2].push_back({i,j});
            }
            if (c[i][j]=='4')
            {
                active[3].push_back({i,j});
            }
            if (c[i][j]=='5')
            {
                active[4].push_back({i,j});
            }
            if (c[i][j]=='6')
            {
                active[5].push_back({i,j});
            }
            if (c[i][j]=='7')
            {
                active[6].push_back({i,j});
            }
            if (c[i][j]=='8')
            {
                active[7].push_back({i,j});
            }
            if (c[i][j]=='9')
            {
                active[8].push_back({i,j});
            }
            j++;
        }
        i++;
    }
    while (sk<n*m)
    {
        i=0;
        while (i<p&&sk<n*m)
        {
            j=0;
            while (j<s[i]&&sk<n*m&&active[i].size())
            {
                for (pair<int,int> j:active[i])
                {
                    if (j.first-1>=0&&c[j.first-1][j.second]=='.')
                    {
                        c[j.first-1][j.second]=i+'1';
                        x.push_back({j.first-1,j.second});
                        sk++;
                    }
                    if (j.second-1>=0&&c[j.first][j.second-1]=='.')
                    {
                        c[j.first][j.second-1]=i+'1';
                        x.push_back({j.first,j.second-1});
                        sk++;
                    }
                    if (j.first+1<=n&&c[j.first+1][j.second]=='.')
                    {
                        c[j.first+1][j.second]=i+'1';
                        x.push_back({j.first+1,j.second});
                        sk++;
                    }
                    if (j.second+1<=m&&c[j.first][j.second+1]=='.')
                    {
                        c[j.first][j.second+1]=i+'1';
                        x.push_back({j.first,j.second+1});
                        sk++;
                    }
                }
                active[i]=x;
                x.clear();
                j++;
            }
            i++;
        }
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            if (c[i][j]!='.'&&c[i][j]!='#')
            {
                rez[c[i][j]-'1']++;
            }
            j++;
        }
        i++;
    }
    i=0;
    while (i<p)
    {
        cout << rez[i] << " ";
        i++;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int i,j,k,n,m,p,s[10],sk,rez[10];
char c[1003][1003];
list<pair<int,int> > active[10],x;
int main()
{
    std::cin.sync_with_stdio(false);
    //std::cin.tie(0);
    cin >> n >> m >> p;
    i=0;
    while (i<p)
    {
        cin >> s[i];
        i++;
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            cin >> c[i][j];
            if (c[i][j]!='.')
            {
                sk++;
            }
            if (c[i][j]=='1')
            {
                active[0].push_back({i,j});
            }
            if (c[i][j]=='2')
            {
                active[1].push_back({i,j});
            }
            if (c[i][j]=='3')
            {
                active[2].push_back({i,j});
            }
            if (c[i][j]=='4')
            {
                active[3].push_back({i,j});
            }
            if (c[i][j]=='5')
            {
                active[4].push_back({i,j});
            }
            if (c[i][j]=='6')
            {
                active[5].push_back({i,j});
            }
            if (c[i][j]=='7')
            {
                active[6].push_back({i,j});
            }
            if (c[i][j]=='8')
            {
                active[7].push_back({i,j});
            }
            if (c[i][j]=='9')
            {
                active[8].push_back({i,j});
            }
            j++;
        }
        i++;
    }
    while (sk<n*m)
    {
        i=0;
        while (i<p&&sk<n*m)
        {
            j=0;
            while (j<s[i]&&sk<n*m&&active[i].size())
            {
                for (pair<int,int> j:active[i])
                {
                    if (j.first-1>=0&&c[j.first-1][j.second]=='.')
                    {
                        c[j.first-1][j.second]=i+'1';
                        x.push_back({j.first-1,j.second});
                        sk++;
                    }
                    if (j.second-1>=0&&c[j.first][j.second-1]=='.')
                    {
                        c[j.first][j.second-1]=i+'1';
                        x.push_back({j.first,j.second-1});
                        sk++;
                    }
                    if (j.first+1<=n&&c[j.first+1][j.second]=='.')
                    {
                        c[j.first+1][j.second]=i+'1';
                        x.push_back({j.first+1,j.second});
                        sk++;
                    }
                    if (j.second+1<=m&&c[j.first][j.second+1]=='.')
                    {
                        c[j.first][j.second+1]=i+'1';
                        x.push_back({j.first,j.second+1});
                        sk++;
                    }
                }
                active[i]=x;
                x.clear();
                j++;
            }
            i++;
        }
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            if (c[i][j]!='.'&&c[i][j]!='#')
            {
                rez[c[i][j]-'1']++;
            }
            j++;
        }
        i++;
    }
    i=0;
    while (i<p)
    {
        cout << rez[i] << " ";
        i++;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int i,j,k,n,m,p,s[10],sk,rez[10];
char c[1003][1003];
vector<pair<int,int> > active[10],x;
int main()
{
    std::cin.sync_with_stdio(false);
    //std::cin.tie(0);
    cin >> n >> m >> p;
    i=0;
    while (i<p)
    {
        cin >> s[i];
        i++;
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            cin >> c[i][j];
            if (c[i][j]!='.')
            {
                sk++;
            }
            if (c[i][j]=='1')
            {
                active[0].push_back({i,j});
            }
            if (c[i][j]=='2')
            {
                active[1].push_back({i,j});
            }
            if (c[i][j]=='3')
            {
                active[2].push_back({i,j});
            }
            if (c[i][j]=='4')
            {
                active[3].push_back({i,j});
            }
            if (c[i][j]=='5')
            {
                active[4].push_back({i,j});
            }
            if (c[i][j]=='6')
            {
                active[5].push_back({i,j});
            }
            if (c[i][j]=='7')
            {
                active[6].push_back({i,j});
            }
            if (c[i][j]=='8')
            {
                active[7].push_back({i,j});
            }
            if (c[i][j]=='9')
            {
                active[8].push_back({i,j});
            }
            j++;
        }
        i++;
    }
    while (sk<n*m)
    {
        i=0;
        while (i<p&&sk<n*m)
        {
            jj=0;
            while (jj<s[i]&&sk<n*m&&active[i].size())
            {
                for (pair<int,int> j:active[i])
                {
                    if (j.first-1>=0&&c[j.first-1][j.second]=='.')
                    {
                        c[j.first-1][j.second]=i+'1';
                        x.push_back({j.first-1,j.second});
                        sk++;
                    }
                    if (j.second-1>=0&&c[j.first][j.second-1]=='.')
                    {
                        c[j.first][j.second-1]=i+'1';
                        x.push_back({j.first,j.second-1});
                        sk++;
                    }
                    if (j.first+1<=n&&c[j.first+1][j.second]=='.')
                    {
                        c[j.first+1][j.second]=i+'1';
                        x.push_back({j.first+1,j.second});
                        sk++;
                    }
                    if (j.second+1<=m&&c[j.first][j.second+1]=='.')
                    {
                        c[j.first][j.second+1]=i+'1';
                        x.push_back({j.first,j.second+1});
                        sk++;
                    }
                }
                active[i]=x;
                x.clear();
                jj++;
            }
            i++;
        }
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            if (c[i][j]!='.'&&c[i][j]!='#')
            {
                rez[c[i][j]-'1']++;
            }
            j++;
        }
        i++;
    }
    i=0;
    while (i<p)
    {
        cout << rez[i] << " ";
        i++;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int i,j,k,n,m,p,s[10],sk,rez[10];
char c[1003][1003];
list<pair<int,int> > active[10],x;
int main()
{
    //std::cin.sync_with_stdio(false);
    //std::cin.tie(0);
    cin >> n >> m >> p;
    i=0;
    while (i<p)
    {
        cin >> s[i];
        i++;
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            cin >> c[i][j];
            if (c[i][j]!='.')
            {
                sk++;
            }
            if (c[i][j]=='1')
            {
                active[0].push_back({i,j});
            }
            if (c[i][j]=='2')
            {
                active[1].push_back({i,j});
            }
            if (c[i][j]=='3')
            {
                active[2].push_back({i,j});
            }
            if (c[i][j]=='4')
            {
                active[3].push_back({i,j});
            }
            if (c[i][j]=='5')
            {
                active[4].push_back({i,j});
            }
            if (c[i][j]=='6')
            {
                active[5].push_back({i,j});
            }
            if (c[i][j]=='7')
            {
                active[6].push_back({i,j});
            }
            if (c[i][j]=='8')
            {
                active[7].push_back({i,j});
            }
            if (c[i][j]=='9')
            {
                active[8].push_back({i,j});
            }
            j++;
        }
        i++;
    }
    while (sk<n*m)
    {
        i=0;
        while (i<p)
        {
            j=0;
            while (j<s[i])
            {
                for (pair<int,int> j:active[i])
                {
                    if (j.first-1>=0&&c[j.first-1][j.second]=='.')
                    {
                        c[j.first-1][j.second]=i+'1';
                        x.push_back({j.first-1,j.second});
                        sk++;
                    }
                    if (j.second-1>=0&&c[j.first][j.second-1]=='.')
                    {
                        c[j.first][j.second-1]=i+'1';
                        x.push_back({j.first,j.second-1});
                        sk++;
                    }
                    if (j.first+1<=n&&c[j.first+1][j.second]=='.')
                    {
                        c[j.first+1][j.second]=i+'1';
                        x.push_back({j.first+1,j.second});
                        sk++;
                    }
                    if (j.second+1<=m&&c[j.first][j.second+1]=='.')
                    {
                        c[j.first][j.second+1]=i+'1';
                        x.push_back({j.first,j.second+1});
                        sk++;
                    }
                }
                active[i]=x;
                x.clear();
                j++;
            }
            i++;
        }
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            if (c[i][j]!='.'&&c[i][j]!='#')
            {
                rez[c[i][j]-'1']++;
            }
            j++;
        }
        i++;
    }
    i=0;
    while (i<p)
    {
        cout << rez[i] << " ";
        i++;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int i,j,k,n,m,p,s[10],sk,rez[10];
char c[1003][1003];
vector<pair<int,int> > active[10],x;
int main()
{
    std::cin.sync_with_stdio(false);
    //std::cin.tie(0);
    cin >> n >> m >> p;
    i=0;
    while (i<p)
    {
        cin >> s[i];
        i++;
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            cin >> c[i][j];
            if (c[i][j]!='.')
            {
                sk++;
            }
            if (c[i][j]=='1')
            {
                active[0].push_back({i,j});
            }
            if (c[i][j]=='2')
            {
                active[1].push_back({i,j});
            }
            if (c[i][j]=='3')
            {
                active[2].push_back({i,j});
            }
            if (c[i][j]=='4')
            {
                active[3].push_back({i,j});
            }
            if (c[i][j]=='5')
            {
                active[4].push_back({i,j});
            }
            if (c[i][j]=='6')
            {
                active[5].push_back({i,j});
            }
            if (c[i][j]=='7')
            {
                active[6].push_back({i,j});
            }
            if (c[i][j]=='8')
            {
                active[7].push_back({i,j});
            }
            if (c[i][j]=='9')
            {
                active[8].push_back({i,j});
            }
            j++;
        }
        i++;
    }
    while (sk<n*m)
    {
        i=0;
        while (i<p&&sk<n*m)
        {
            j=0;
            while (j<s[i]&&sk<n*m&&active[i].size())
            {
                for (pair<int,int> j:active[i])
                {
                    if (j.first-1>=0&&c[j.first-1][j.second]=='.')
                    {
                        c[j.first-1][j.second]=i+'1';
                        x.push_back({j.first-1,j.second});
                        sk++;
                    }
                    if (j.second-1>=0&&c[j.first][j.second-1]=='.')
                    {
                        c[j.first][j.second-1]=i+'1';
                        x.push_back({j.first,j.second-1});
                        sk++;
                    }
                    if (j.first+1<=n&&c[j.first+1][j.second]=='.')
                    {
                        c[j.first+1][j.second]=i+'1';
                        x.push_back({j.first+1,j.second});
                        sk++;
                    }
                    if (j.second+1<=m&&c[j.first][j.second+1]=='.')
                    {
                        c[j.first][j.second+1]=i+'1';
                        x.push_back({j.first,j.second+1});
                        sk++;
                    }
                }
                active[i]=x;
                x.clear();
                j++;
            }
            i++;
        }
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            if (c[i][j]!='.'&&c[i][j]!='#')
            {
                rez[c[i][j]-'1']++;
            }
            j++;
        }
        i++;
    }
    i=0;
    while (i<p)
    {
        cout << rez[i] << " ";
        i++;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int i,jj,j,k,n,m,p,s[10],sk,rez[10];
char c[1003][1003];
vector<pair<int,int> > active[10],x;
int main()
{
    std::cin.sync_with_stdio(false);
    //std::cin.tie(0);
    cin >> n >> m >> p;
    i=0;
    while (i<p)
    {
        cin >> s[i];
        i++;
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            cin >> c[i][j];
            if (c[i][j]!='.')
            {
                sk++;
            }
            if (c[i][j]=='1')
            {
                active[0].push_back({i,j});
            }
            if (c[i][j]=='2')
            {
                active[1].push_back({i,j});
            }
            if (c[i][j]=='3')
            {
                active[2].push_back({i,j});
            }
            if (c[i][j]=='4')
            {
                active[3].push_back({i,j});
            }
            if (c[i][j]=='5')
            {
                active[4].push_back({i,j});
            }
            if (c[i][j]=='6')
            {
                active[5].push_back({i,j});
            }
            if (c[i][j]=='7')
            {
                active[6].push_back({i,j});
            }
            if (c[i][j]=='8')
            {
                active[7].push_back({i,j});
            }
            if (c[i][j]=='9')
            {
                active[8].push_back({i,j});
            }
            j++;
        }
        i++;
    }
    while (sk<n*m)
    {
        i=0;
        while (i<p&&sk<n*m)
        {
            jj=0;
            while (jj<s[i]&&sk<n*m&&active[i].size())
            {
                for (pair<int,int> j:active[i])
                {
                    if (j.first-1>=0&&c[j.first-1][j.second]=='.')
                    {
                        c[j.first-1][j.second]=i+'1';
                        x.push_back({j.first-1,j.second});
                        sk++;
                    }
                    if (j.second-1>=0&&c[j.first][j.second-1]=='.')
                    {
                        c[j.first][j.second-1]=i+'1';
                        x.push_back({j.first,j.second-1});
                        sk++;
                    }
                    if (j.first+1<=n&&c[j.first+1][j.second]=='.')
                    {
                        c[j.first+1][j.second]=i+'1';
                        x.push_back({j.first+1,j.second});
                        sk++;
                    }
                    if (j.second+1<=m&&c[j.first][j.second+1]=='.')
                    {
                        c[j.first][j.second+1]=i+'1';
                        x.push_back({j.first,j.second+1});
                        sk++;
                    }
                }
                active[i]=x;
                x.clear();
                jj++;
            }
            i++;
        }
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            if (c[i][j]!='.'&&c[i][j]!='#')
            {
                rez[c[i][j]-'1']++;
            }
            j++;
        }
        i++;
    }
    i=0;
    while (i<p)
    {
        cout << rez[i] << " ";
        i++;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int i,j,k,n,m,p,s[10],sk,rez[10];
char c[1003][1003];
list<pair<int,int> > active[10],x;
int main()
{
    //std::cin.sync_with_stdio(false);
    //std::cin.tie(0);
    cin >> n >> m >> p;
    i=0;
    while (i<p)
    {
        cin >> s[i];
        i++;
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            cin >> c[i][j];
            if (c[i][j]!='.')
            {
                sk++;
            }
            if (c[i][j]=='1')
            {
                active[0].push_back({i,j});
            }
            if (c[i][j]=='2')
            {
                active[1].push_back({i,j});
            }
            if (c[i][j]=='3')
            {
                active[2].push_back({i,j});
            }
            if (c[i][j]=='4')
            {
                active[3].push_back({i,j});
            }
            if (c[i][j]=='5')
            {
                active[4].push_back({i,j});
            }
            if (c[i][j]=='6')
            {
                active[5].push_back({i,j});
            }
            if (c[i][j]=='7')
            {
                active[6].push_back({i,j});
            }
            if (c[i][j]=='8')
            {
                active[7].push_back({i,j});
            }
            if (c[i][j]=='9')
            {
                active[8].push_back({i,j});
            }
            j++;
        }
        i++;
    }
    while (sk<n*m)
    {
        i=0;
        while (i<p&&sk<n*m)
        {
            j=0;
            while (j<s[i]&&sk<n*m)
            {
                for (pair<int,int> j:active[i])
                {
                    if (j.first-1>=0&&c[j.first-1][j.second]=='.')
                    {
                        c[j.first-1][j.second]=i+'1';
                        x.push_back({j.first-1,j.second});
                        sk++;
                    }
                    if (j.second-1>=0&&c[j.first][j.second-1]=='.')
                    {
                        c[j.first][j.second-1]=i+'1';
                        x.push_back({j.first,j.second-1});
                        sk++;
                    }
                    if (j.first+1<=n&&c[j.first+1][j.second]=='.')
                    {
                        c[j.first+1][j.second]=i+'1';
                        x.push_back({j.first+1,j.second});
                        sk++;
                    }
                    if (j.second+1<=m&&c[j.first][j.second+1]=='.')
                    {
                        c[j.first][j.second+1]=i+'1';
                        x.push_back({j.first,j.second+1});
                        sk++;
                    }
                }
                active[i]=x;
                x.clear();
                j++;
            }
            i++;
        }
    }
    i=0;
    while (i<n)
    {
        j=0;
        while (j<m)
        {
            if (c[i][j]!='.'&&c[i][j]!='#')
            {
                rez[c[i][j]-'1']++;
            }
            j++;
        }
        i++;
    }
    i=0;
    while (i<p)
    {
        cout << rez[i] << " ";
        i++;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PSET(x,y) fixed<<setprecision(y)<<x
#define pb push_back
#define pf push_front
#define mp make_pair
#define pii pair<int,int>
#define pli pair<int,int>
#define pint pair<int,int>
#define pid pair<int,double>
#define vi vector<int>
#define ff first
#define ss second
#define ll long long
#define ull unsigned long long
#define SIZE 200010
#define MOD 1000000007
#define BIG 999999937

ll n,m,p;
int s[10];
string a[1001];
int occ = 0;
bool vis[1001][1001] = {false};
queue < pair<pii,int> > q[10];
int ans[10] = {0};

void func(int curr)
{
  if ( q[curr].empty() ) return;

  int tot = s[curr];
  int prev = (q[curr].front()).ss;

  int x[] = {-1,0,1, 0};
  int y[] = { 0, 1, 0,-1};
  while ( !q[curr].empty() )
  {
    pair < pii, int > t = q[curr].front();

    if ( t.ss - prev == s[curr] ) break;
    int i = t.ff.ff,j = t.ff.ss;

    //cout<<curr<<" "<<i<<" "<<j<<endl;

    q[curr].pop();

    for (int k = 0;k<4;k++)
    {
      int new_i = i + y[k], new_j = j + x[k];
      if ( new_i <= 0 or new_i > n ) continue;
      if ( new_j <= 0 or new_j > m ) continue;
      if ( vis[new_i][new_j] ) continue;
      if ( a[new_i][new_j] == '#' ) continue;
      vis[new_i][new_j] = true;
      occ++;
      q[curr].push( mp( mp(new_i, new_j), t.ss + 1 ) );
      ans[curr]++;
    }

  }

}

int main()
{
    fastio()

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    cin>>n>>m>>p;
    for (int i = 1;i<=p;i++) cin>>s[i];
    for (int i = 1;i<=n;i++) {
      cin>>a[i];
      a[i] = '#' + a[i];
    }

    for (int i = 1;i<=n;i++)
    {
      for (int j = 1;j<=m;j++)
      {
        if ( a[i][j] == '#' ) {
            occ++;
            continue;
        }
        if ( a[i][j] == '.' ) continue;
        vis[i][j] = true;
        int dig = a[i][j] - '0';
        q[dig].push( mp( mp(i,j), 0 ) );
        ans[dig]++;
        occ++;
      }
    }
    //cout<<occ<<endl;
    while ( occ < n*m )
    {
      for (int i = 1;i<=p;i++) func(i);
    }

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

  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PSET(x,y) fixed<<setprecision(y)<<x
#define pb push_back
#define pf push_front
#define mp make_pair
#define pii pair<int,int>
#define pli pair<int,int>
#define pint pair<int,int>
#define pid pair<int,double>
#define vi vector<int>
#define ff first
#define ss second
#define ll long long
#define ull unsigned long long
#define SIZE 200010
#define MOD 1000000007
#define BIG 999999937

ll n,m,p;
int s[10];
string a[1001];
int occ = 0;
bool vis[1001][1001] = {false};
queue < pair<pii,int> > q[10];
int ans[10] = {0};

void func(int curr)
{
  int tot = s[curr];
  for (int i = 0;i<q[curr].size();i++)
  {
    pair < pii, int > t = q[curr].front();
    q[curr].pop();
    q[curr].push( mp(t.ff, 0) );
  }

  int x[] = {-1,0,1, 0};
  int y[] = { 0, 1, 0,-1};
  while ( !q[curr].empty() )
  {
    pair < pii, int > t = q[curr].front();
    if ( t.ss == s[curr] ) break;
    q[curr].pop();
    int i = t.ff.ff,j = t.ff.ss;
    for (int k = 0;k<4;k++)
    {
      int new_i = i + y[k], new_j = j + x[k];
      if ( new_i <= 0 or new_i > n ) continue;
      if ( new_j <= 0 or new_j > m ) continue;
      if ( vis[new_i][new_j] ) continue;
      if ( a[new_i][new_j] == '#' ) continue;
      vis[new_i][new_j] = true;
      occ++;
      q[curr].push( mp( mp(new_i, new_j), t.ss + 1 ) );
      ans[curr]++;
    }

  }

}

int main()
{
    fastio()

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    cin>>n>>m>>p;
    for (int i = 1;i<=p;i++) cin>>s[i];
    for (int i = 1;i<=n;i++) {
      cin>>a[i];
      a[i] = '#' + a[i];
    }

    for (int i = 1;i<=n;i++)
    {
      for (int j = 1;j<=m;j++)
      {
        if ( a[i][j] == '#' ) {
            occ++;
            continue;
        }
        if ( a[i][j] == '.' ) continue;
        vis[i][j] = true;
        int dig = a[i][j] - '0';
        q[dig].push( mp( mp(i,j), 0 ) );
        ans[dig]++;
        occ++;
      }
    }
    //cout<<occ<<endl;
    while ( occ < n*m )
    {
      for (int i = 1;i<=p;i++) func(i);
    }

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

  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PSET(x,y) fixed<<setprecision(y)<<x
#define pb push_back
#define pf push_front
#define mp make_pair
#define pii pair<ll,ll>
#define pli pair<int,int>
#define pint pair<int,int>
#define pid pair<int,double>
#define vi vector<int>
#define ff first
#define ss second
#define ll long long
#define ull unsigned long long
#define SIZE 200010
#define MOD 1000000007
#define BIG 999999937

vector < pii > v[10][1000001];
ll n,m,p;
ll s[10];
string a[1001];

vector <pii> st[10];

void bfs(int p)
{
  bool vis[1001][1001] = {false};

  int x[] = {-1,0,1, 0};
  int y[] = {0, 1, 0, -1};

  queue < pair< pair<ll,ll> ,ll > > q;

  for (auto src : st[p]) {
    q.push( mp( src, 0 ) );
    vis[src.ff][src.ss] = true;
  }

  while (!q.empty() ) {
    pair< pii,ll > t = q.front();
    ll i = t.ff.ff, j = t.ff.ss;
    q.pop();
    v[p][t.ss].push_back( mp(i,j) );
    for (ll k = 0;k<4;k++) {
      if ( i + x[k] > n or i + x[k] <= 0 ) continue;
      if ( j + y[k] > m or j + y[k] <= 0 ) continue;
      if ( vis[ i + x[k] ][ j + y[k] ] ) continue;
      if ( a[ i + x[k] ][ j + y[k] ] == '#' ) continue;
      vis[ i + x[k] ][ j + y[k] ] = true;
      q.push( mp( mp( i+x[k], j + y[k] ) , t.ss + 1 ) );

    }
  }
}

int main()
{
    fastio()

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    cin>>n>>m>>p;
    for (int i = 1;i<=p;i++) cin>>s[i];

    for (int i = 1;i<=n;i++) {
      cin>>a[i];
      a[i] = '#' + a[i];
    }

    for (int i = 1;i<=n;i++) {
      for (int j = 1;j<=m;j++) {
        if ( a[i][j] == '.' or a[i][j] == '#' ) continue;
        st[ a[i][j] - '0' ].push_back( mp(i,j) );
      }
    }

    for (int i = 1;i<=p;i++) bfs( i );

    ll ans[11];
    memset( ans, 0, sizeof(ans) );
    ll occ = 0;
    bool vis[1001][1001] = {false};

    ll turn = 0;
    for (int i = 1;i<=p;i++) {
      ans[i] = st[i].size();

      for (auto j : st[i]) {
        vis[ j.ff ][ j.ss ] = true;
        occ++;
      }
    }
    for (int i = 1;i<=n;i++) {
      for (int j = 1;j<=m;j++) {
        if ( a[i][j] == '#' ) occ++;
      }
    }

    // int i = 0;
    // while ( v[2][i].size() != 0 ) {
    //   cout<<i<<" -> ";
    //   for (auto j : v[2][i]) cout<<j.ff<<" "<<j.ss<<endl;
    //     i++;
    // }

    bool canMove[11];
    for (int i = 1;i<=p;i++) canMove[i] = true;

    while ( occ < n*m  )
    {
      turn++;
      for (int i = 1;i<=p;i++) {
        if ( !canMove[i] ) continue;
        for (ll j = s[i]*(turn-1) + 1; j<=s[i]*turn and j<=n*m; j++) {
          bool temp = false;
          for (auto k : v[i][j]) {
            if ( vis[k.ff][k.ss] ) continue;
            temp = true;
            ans[i]++;
            occ++;
            vis[k.ff][k.ss] = true;
          }
          canMove[i] = canMove[i] || temp;
          if ( temp == false ) break;
        }

      }
    }

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

  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PSET(x,y) fixed<<setprecision(y)<<x
#define pb push_back
#define pf push_front
#define mp make_pair
#define pii pair<int,int>
#define pli pair<int,int>
#define pint pair<int,int>
#define pid pair<int,double>
#define vi vector<int>
#define ff first
#define ss second
#define ll long long
#define ull unsigned long long
#define SIZE 200010
#define MOD 1000000007
#define BIG 999999937

ll n,m,p;
int s[10];
string a[1001];
int occ = 0;
bool vis[1001][1001] = {false};
set < pii > st[10];
int ans[10] = {0};

void func(int curr)
{
  if ( st[curr].empty() ) return;

  int tot = s[curr];

  queue < pii > q;
  while ( !st[curr].empty() ) {
    q.push( *st[curr].begin() );
    st[curr].erase( st[curr].begin() );
  }

  int x[] = {-1,0,1, 0};
  int y[] = { 0, 1, 0,-1};
  int dist[1001][1001] = {0};

  while ( !q.empty() )
  {
    pii t = q.front();

    int i = t.ff,j = t.ss;

    for (int k = 1;k<=p;k++) {
      if ( k == curr ) continue;
      st[k].erase( mp(i,j) );
    }

    if ( dist[i][j] == tot ) break;
    //cout<<curr<<" "<<i<<" "<<j<<endl;

    q.pop();

    for (int k = 0;k<4;k++)
    {
      int new_i = i + y[k], new_j = j + x[k];
      if ( new_i <= 0 or new_i > n ) continue;
      if ( new_j <= 0 or new_j > m ) continue;
      if ( vis[new_i][new_j] ) continue;
      if ( a[new_i][new_j] == '#' ) continue;
      vis[new_i][new_j] = true;
      occ++;
      q.push( mp(new_i, new_j) );
      ans[curr]++;
      dist[new_i][new_j] = dist[i][j] + 1;
    }

  }
  while ( !q.empty() ) {
    st[curr].insert( q.front() );
    q.pop();
  }

}

int main()
{
    fastio()

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    cin>>n>>m>>p;
    for (int i = 1;i<=p;i++) cin>>s[i];
    for (int i = 1;i<=n;i++) {
      cin>>a[i];
      a[i] = '#' + a[i];
    }

    for (int i = 1;i<=n;i++)
    {
      for (int j = 1;j<=m;j++)
      {
        if ( a[i][j] == '#' ) {
            occ++;
            continue;
        }
        if ( a[i][j] == '.' ) continue;
        vis[i][j] = true;
        int dig = a[i][j] - '0';
        st[dig].insert( mp(i,j) );
        ans[dig]++;
        occ++;
      }
    }
    //cout<<occ<<endl;
    while ( occ < n*m )
    {
      for (int i = 1;i<=p;i++) func(i);
    }

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

  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PSET(x,y) fixed<<setprecision(y)<<x
#define pb push_back
#define pf push_front
#define mp make_pair
#define pii pair<ll,ll>
#define pli pair<int,int>
#define pint pair<int,int>
#define pid pair<int,double>
#define vi vector<int>
#define ff first
#define ss second
#define ll long long
#define ull unsigned long long
#define SIZE 200010
#define MOD 1000000007
#define BIG 999999937

vector < pii > v[10][1000001];
ll n,m,p;
ll s[10];
string a[1001];

vector <pii> st[10];

void bfs(int p)
{
  bool vis[1001][1001] = {false};

  int x[] = {-1,0,1, 0};
  int y[] = {0, 1, 0, -1};

  queue < pair< pair<ll,ll> ,ll > > q;

  for (auto src : st[p]) {
    q.push( mp( src, 0 ) );
    vis[src.ff][src.ss] = true;
  }

  while (!q.empty() ) {
    pair< pii,ll > t = q.front();
    ll i = t.ff.ff, j = t.ff.ss;
    q.pop();
    v[p][t.ss].push_back( mp(i,j) );
    for (ll k = 0;k<4;k++) {
      if ( i + x[k] > n or i + x[k] <= 0 ) continue;
      if ( j + y[k] > m or j + y[k] <= 0 ) continue;
      if ( vis[ i + x[k] ][ j + y[k] ] ) continue;
      if ( a[ i + x[k] ][ j + y[k] ] == '#' ) continue;
      vis[ i + x[k] ][ j + y[k] ] = true;
      q.push( mp( mp( i+x[k], j + y[k] ) , t.ss + 1 ) );

    }
  }
}

int main()
{
    fastio()

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    cin>>n>>m>>p;
    for (int i = 1;i<=p;i++) cin>>s[i];

    for (int i = 1;i<=n;i++) {
      cin>>a[i];
      a[i] = '#' + a[i];
    }
    int kiska[1001][1001] = {0};

    for (int i = 1;i<=n;i++) {
      for (int j = 1;j<=m;j++) {
        if ( a[i][j] == '.' or a[i][j] == '#' ) continue;
        st[ a[i][j] - '0' ].push_back( mp(i,j) );
      }
    }

    for (int i = 1;i<=p;i++) bfs( i );

    ll ans[11];
    memset( ans, 0, sizeof(ans) );
    int kabza[1001][1001] = {0};

    for (int i = 1;i<=p;i++) {
      for (int j = 0;j<=n*m;j++) {
        bool canMove = false;
        for (auto k : v[i][j]) {
            int turn = j/s[i];
            if ( j % s[i] != 0 ) turn++;
            if ( kiska[k.ff][k.ss] == 0 ) {
              kiska[k.ff][k.ss] = i;
              ans[i]++;
              kabza[k.ff][k.ss] = turn;
              canMove = true;
            }
            else if ( kabza[k.ff][k.ss] > turn ) {
              ans[ kiska[k.ff][k.ss] ]--;
              kiska[k.ff][k.ss] = i;
              ans[i]++;
              kabza[k.ff][k.ss] = turn;
              canMove = true;
            }
          }
          if ( !canMove ) break;
        }

      }

      // for (int i = 1;i<=n;i++) {
      //   for (int j = 1;j<=m;j++) cout<<kabza[i][j];
      //   cout<<endl;
      //}

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

  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PSET(x,y) fixed<<setprecision(y)<<x
#define pb push_back
#define pf push_front
#define mp make_pair
#define pii pair<ll,ll>
#define pli pair<int,int>
#define pint pair<int,int>
#define pid pair<int,double>
#define vi vector<int>
#define ff first
#define ss second
#define ll long long
#define ull unsigned long long
#define SIZE 200010
#define MOD 1000000007
#define BIG 999999937

vector < pii > v[10][1000001];
ll n,m,p;
ll s[10];
string a[1001];

vector <pii> st[10];

void bfs(int p)
{
  bool vis[1001][1001] = {false};

  int x[] = {-1,0,1, 0};
  int y[] = {0, 1, 0, -1};

  queue < pair< pair<ll,ll> ,ll > > q;

  for (auto src : st[p]) {
    q.push( mp( src, 0 ) );
    vis[src.ff][src.ss] = true;
  }

  while (!q.empty() ) {
    pair< pii,ll > t = q.front();
    ll i = t.ff.ff, j = t.ff.ss;
    q.pop();
    v[p][t.ss].push_back( mp(i,j) );
    for (ll k = 0;k<4;k++) {
      if ( i + x[k] > n or i + x[k] <= 0 ) continue;
      if ( j + y[k] > m or j + y[k] <= 0 ) continue;
      if ( vis[ i + x[k] ][ j + y[k] ] ) continue;
      if ( a[ i + x[k] ][ j + y[k] ] == '#' ) continue;
      vis[ i + x[k] ][ j + y[k] ] = true;
      q.push( mp( mp( i+x[k], j + y[k] ) , t.ss + 1 ) );

    }
  }
}

int main()
{
    fastio()

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    cin>>n>>m>>p;
    for (int i = 1;i<=p;i++) cin>>s[i];

    for (int i = 1;i<=n;i++) {
      cin>>a[i];
      a[i] = '#' + a[i];
    }

    for (int i = 1;i<=n;i++) {
      for (int j = 1;j<=m;j++) {
        if ( a[i][j] == '.' or a[i][j] == '#' ) continue;
        st[ a[i][j] - '0' ].push_back( mp(i,j) );
      }
    }

    for (int i = 1;i<=p;i++) bfs( i );

    ll ans[11];
    memset( ans, 0, sizeof(ans) );
    ll occ = 0;
    bool vis[1001][1001] = {false};

    ll turn = 0;
    for (int i = 1;i<=p;i++) {
      ans[i] = st[i].size();

      for (auto j : st[i]) {
        vis[ j.ff ][ j.ss ] = true;
        occ++;
      }
    }
    for (int i = 1;i<=n;i++) {
      for (int j = 1;j<=m;j++) {
        if ( a[i][j] == '#' ) occ++;
      }
    }

    // int i = 0;
    // while ( v[2][i].size() != 0 ) {
    //   cout<<i<<" -> ";
    //   for (auto j : v[2][i]) cout<<j.ff<<" "<<j.ss<<endl;
    //     i++;
    // }

    while ( occ < n*m  )
    {
      turn++;
      for (int i = 1;i<=p;i++) {

        for (ll j = s[i]*(turn-1) + 1; j<=s[i]*turn and j<=n*m; j++) {
          bool temp = false;
          for (auto k : v[i][j]) {
            if ( vis[k.ff][k.ss] ) continue;
            temp = true;
            ans[i]++;
            occ++;
            vis[k.ff][k.ss] = true;
          }
          if ( temp == false ) break;
        }

      }
    }

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

  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PSET(x,y) fixed<<setprecision(y)<<x
#define pb push_back
#define pf push_front
#define mp make_pair
#define pii pair<ll,ll>
#define pli pair<int,int>
#define pint pair<int,int>
#define pid pair<int,double>
#define vi vector<int>
#define ff first
#define ss second
#define ll long long
#define ull unsigned long long
#define SIZE 200010
#define MOD 1000000007
#define BIG 999999937

vector < pii > v[10][1000001];
ll n,m,p;
ll s[10];
string a[1001];

pii st[10];

void bfs(pii src, int p)
{
  bool vis[1001][1001] = {false};

  int x[] = {-1,0,1, 0};
  int y[] = {0, 1, 0, -1};

  queue < pair< pair<ll,ll> ,ll > > q;
  q.push( mp( src, 0 ) );
  vis[src.ff][src.ss] = true;

  while (!q.empty() ) {
    pair< pii,ll > t = q.front();
    ll i = t.ff.ff, j = t.ff.ss;
    q.pop();
    v[p][t.ss].push_back( mp(i,j) );
    for (ll k = 0;k<4;k++) {
      if ( i + x[k] > n or i + x[k] <= 0 ) continue;
      if ( j + y[k] > m or j + y[k] <= 0 ) continue;
      if ( vis[ i + x[k] ][ j + y[k] ] ) continue;
      if ( a[ i + x[k] ][ j + y[k] ] == '#' ) continue;
      vis[ i + x[k] ][ j + y[k] ] = true;
      q.push( mp( mp( i+x[k], j + y[k] ) , t.ss + 1 ) );

    }
  }
}

int main()
{
    fastio()

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    cin>>n>>m>>p;
    for (int i = 1;i<=p;i++) cin>>s[i];

    for (int i = 1;i<=n;i++) {
      cin>>a[i];
      a[i] = '#' + a[i];
    }

    for (int i = 1;i<=n;i++) {
      for (int j = 1;j<=m;j++) {
        if ( a[i][j] == '.' or a[i][j] == '#' ) continue;
        st[ a[i][j] - '0' ] = mp(i,j);
      }
    }

    for (int i = 1;i<=p;i++) bfs( st[i], i );

    ll ans[11];
    memset( ans, 0, sizeof(ans) );
    ll occ = 0;
    bool vis[1001][1001] = {false};

    ll turn = 0;
    for (int i = 1;i<=p;i++) {
      ans[i]++;
      occ++;
      vis[ st[i].ff ][ st[i].ss ] = true;
    }
    for (int i = 1;i<=n;i++) {
      for (int j = 1;j<=m;j++) {
        if ( a[i][j] == '#' ) occ++;
      }
    }

    // int i = 0;
    // while ( v[2][i].size() != 0 ) {
    //   cout<<i<<" -> ";
    //   for (auto j : v[2][i]) cout<<j.ff<<" "<<j.ss<<endl;
    //     i++;
    // }

    while ( occ < n*m  )
    {
      turn++;
      for (int i = 1;i<=p;i++) {

        for (ll j = s[i]*(turn-1) + 1; j<=s[i]*turn and j<=n*m; j++) {
          bool temp = false;
          for (auto k : v[i][j]) {
            if ( vis[k.ff][k.ss] ) continue;
            temp = true;
            ans[i]++;
            occ++;
            vis[k.ff][k.ss] = true;
          }
          if ( temp == false ) break;
        }

      }
    }

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

  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PSET(x,y) fixed<<setprecision(y)<<x
#define pb push_back
#define pf push_front
#define mp make_pair
#define pii pair<int,int>
#define pli pair<int,int>
#define pint pair<int,int>
#define pid pair<int,double>
#define vi vector<int>
#define ff first
#define ss second
#define ll long long
#define ull unsigned long long
#define SIZE 200010
#define MOD 1000000007
#define BIG 999999937

ll n,m,p;
int s[10];
string a[1001];
int occ = 0;
bool vis[1001][1001] = {false};
queue < pair<pii,int> > q[10];
int ans[10] = {0};

bool func(int curr)
{
  if ( q[curr].empty() ) return false;

  int tot = s[curr];
  int prev = (q[curr].front()).ss;

  int x[] = {-1,0,1, 0};
  int y[] = { 0, 1, 0,-1};
  while ( !q[curr].empty() )
  {
    pair < pii, int > t = q[curr].front();

    if ( t.ss - prev == s[curr] ) break;
    int i = t.ff.ff,j = t.ff.ss;

    //cout<<curr<<" "<<i<<" "<<j<<endl;

    q[curr].pop();

    for (int k = 0;k<4;k++)
    {
      int new_i = i + y[k], new_j = j + x[k];
      if ( new_i <= 0 or new_i > n ) continue;
      if ( new_j <= 0 or new_j > m ) continue;
      if ( vis[new_i][new_j] ) continue;
      if ( a[new_i][new_j] == '#' ) continue;
      vis[new_i][new_j] = true;
      occ++;
      q[curr].push( mp( mp(new_i, new_j), t.ss + 1 ) );
      ans[curr]++;
    }

  }

  return true;

}

int main()
{
    fastio()

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    cin>>n>>m>>p;
    for (int i = 1;i<=p;i++) cin>>s[i];
    for (int i = 1;i<=n;i++) {
      cin>>a[i];
      a[i] = '#' + a[i];
    }

    for (int i = 1;i<=n;i++)
    {
      for (int j = 1;j<=m;j++)
      {
        if ( a[i][j] == '#' ) {
            occ++;
            continue;
        }
        if ( a[i][j] == '.' ) continue;
        vis[i][j] = true;
        int dig = a[i][j] - '0';
        q[dig].push( mp( mp(i,j), 0 ) );
        ans[dig]++;
        occ++;
      }
    }
    //cout<<occ<<endl;
    while ( occ < n*m )
    {
      bool canMove = false;
      for (int i = 1;i<=p;i++) {
        bool temp = func(i);
        canMove = canMove || temp;
      }
      if ( canMove == false ) break;
    }

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

  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PSET(x,y) fixed<<setprecision(y)<<x
#define pb push_back
#define pf push_front
#define mp make_pair
#define pii pair<int,int>
#define pli pair<int,int>
#define pint pair<int,int>
#define pid pair<int,double>
#define vi vector<int>
#define ff first
#define ss second
#define ll long long
#define ull unsigned long long
#define SIZE 200010
#define MOD 1000000007
#define BIG 999999937

ll n,m,p;
int s[10];
string a[1001];
int occ = 0;
bool vis[1001][1001] = {false};
queue < pair<pii,int> > q[10];
int ans[10] = {0};

bool func(int curr)
{
  if ( q[curr].empty() ) return false;

  int tot = s[curr];
  int prev = (q[curr].front()).ss;

  int x[] = {-1,0,1, 0};
  int y[] = { 0, 1, 0,-1};
  while ( !q[curr].empty() )
  {
    pair < pii, int > t = q[curr].front();

    if ( t.ss - prev == s[curr] ) break;
    int i = t.ff.ff,j = t.ff.ss;

    //cout<<curr<<" "<<i<<" "<<j<<endl;

    q[curr].pop();

    for (int k = 0;k<4;k++)
    {
      int new_i = i + y[k], new_j = j + x[k];
      if ( new_i <= 0 or new_i > n ) continue;
      if ( new_j <= 0 or new_j > m ) continue;
      if ( vis[new_i][new_j] ) continue;
      if ( a[new_i][new_j] == '#' ) continue;
      vis[new_i][new_j] = true;
      occ++;
      q[curr].push( mp( mp(new_i, new_j), t.ss + 1 ) );
      ans[curr]++;
    }

  }
  return true;

}

int main()
{
    fastio()

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    cin>>n>>m>>p;
    for (int i = 1;i<=p;i++) cin>>s[i];
    for (int i = 1;i<=n;i++) {
      cin>>a[i];
      a[i] = '#' + a[i];
    }

    for (int i = 1;i<=n;i++)
    {
      for (int j = 1;j<=m;j++)
      {
        if ( a[i][j] == '#' ) {
            occ++;
            continue;
        }
        if ( a[i][j] == '.' ) continue;
        vis[i][j] = true;
        int dig = a[i][j] - '0';
        q[dig].push( mp( mp(i,j), 0 ) );
        ans[dig]++;
        occ++;
      }
    }
    //cout<<occ<<endl;
    while ( occ < n*m )
    {
      bool canMove = false;
      for (int i = 1;i<=p;i++) canMove = canMove || func(i);
      if ( canMove == false ) break;
    }

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

  return 0;

}


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


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

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