← Home
package main

import (
	"bufio"
	"io"
	"os"
	"strconv"
)

func main() {
	data, _ := io.ReadAll(os.Stdin)
	idx := 0

	nextInt := func() int {
		for idx < len(data) && (data[idx] == ' ' || data[idx] == '\n' || data[idx] == '\r' || data[idx] == '\t') {
			idx++
		}
		val := 0
		for idx < len(data) && data[idx] >= '0' && data[idx] <= '9' {
			val = val*10 + int(data[idx]-'0')
			idx++
		}
		return val
	}

	nextToken := func() []byte {
		for idx < len(data) && (data[idx] == ' ' || data[idx] == '\n' || data[idx] == '\r' || data[idx] == '\t') {
			idx++
		}
		start := idx
		for idx < len(data) && data[idx] != ' ' && data[idx] != '\n' && data[idx] != '\r' && data[idx] != '\t' {
			idx++
		}
		return data[start:idx]
	}

	n := nextInt()
	m := nextInt()
	p := nextInt()

	speeds := make([]int, p)
	for i := 0; i < p; i++ {
		speeds[i] = nextInt()
	}

	grid := make([]byte, n*m)
	queues := make([][]int, p)
	heads := make([]int, p)
	cnt := make([]int, p)

	for i := 0; i < n; i++ {
		row := nextToken()
		base := i * m
		for j := 0; j < m; j++ {
			ch := row[j]
			grid[base+j] = ch
			if ch >= '1' && ch <= '9' {
				player := int(ch - '1')
				queues[player] = append(queues[player], base+j)
				cnt[player]++
			}
		}
	}

	for {
		moved := false
		for player := 0; player < p; player++ {
			q := queues[player]
			h := heads[player]
			if h >= len(q) {
				continue
			}

			for step := 0; step < speeds[player] && h < len(q); step++ {
				levelEnd := len(q)
				for h < levelEnd {
					pos := q[h]
					h++

					r := pos / m
					c := pos - r*m

					if r > 0 {
						np := pos - m
						if grid[np] == '.' {
							grid[np] = byte('1' + player)
							cnt[player]++
							q = append(q, np)
							moved = true
						}
					}
					if r+1 < n {
						np := pos + m
						if grid[np] == '.' {
							grid[np] = byte('1' + player)
							cnt[player]++
							q = append(q, np)
							moved = true
						}
					}
					if c > 0 {
						np := pos - 1
						if grid[np] == '.' {
							grid[np] = byte('1' + player)
							cnt[player]++
							q = append(q, np)
							moved = true
						}
					}
					if c+1 < m {
						np := pos + 1
						if grid[np] == '.' {
							grid[np] = byte('1' + player)
							cnt[player]++
							q = append(q, np)
							moved = true
						}
					}
				}
			}

			queues[player] = q
			heads[player] = h
		}
		if !moved {
			break
		}
	}

	w := bufio.NewWriterSize(os.Stdout, 1<<20)
	for i := 0; i < p; i++ {
		if i > 0 {
			w.WriteByte(' ')
		}
		w.WriteString(strconv.Itoa(cnt[i]))
	}
	w.WriteByte('\n')
	w.Flush()
}