#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <cctype>

using namespace std;

int area = 0;
int rows, columns;

int areas[6] = {0,0,0,0,0,0};

list <char> adj[11];
list <char> charAdj[200];
list <char> temp;
int coords[2][6];
list <char>::iterator it;

string grid[41];
bool tempGrid[41][41];

int dir[4][2] = {{ 0, -1}, {1, 0}, {0,1}, {-1, 0}};


void reset(){
     area = 0;
     for (int fc = 0; fc < 41; ++fc){
         for (int fc2 = 0; fc2 < 41; ++fc2){
             tempGrid[fc][fc2] = false;
         }
     }
}

bool checkBounds(int x, int y){
     if (x >= 0 && x < columns && y >= 0 && y < rows)
        return true;
     return false;
}



void findNumCoords(){
     int fc, fc2, ele;
     for (fc = 0; fc < columns; ++fc){
         for (fc2 =0; fc2 < rows; ++fc2){
             ele = int(grid[fc2][fc]);
             if (isdigit (ele)){
                ele-= 49;
                coords[0][ele] = fc; coords[1][ele] = fc2;
             }
         }                        
     }
}

void findAdjLetters(int x, int y, int number){
     int fc, fc2;
     char ele;
     if (checkBounds(x,y) && grid[y][x] != '#' && tempGrid[y][x] == false){        
        tempGrid[y][x] = true;
        ele = grid[y][x];
        if (isalpha(int(ele))){
           adj[number].push_back(ele);
        }
        for (fc = 0; fc < 4; ++fc){
            findAdjLetters(x + dir[fc][0], y + dir[fc][1], number);
        }
     }
}

void match(){
     int fc, fc2, fc3, fc4, fc5;
     for (fc = 0; fc < 5; ++fc){
         cout<<"room:"<<fc<<" --> ";
         findAdjLetters(coords[0][fc],coords[1][fc],fc);
         cout<<*(adj[fc].begin()); 
     }
}

void findArea(int x, int y){
     int fc, fc2;
     if (checkBounds(x,y) && grid[y][x] != '#' && tempGrid[y][x] == false){
        area++;
        tempGrid[y][x] = true;
        for (fc = 0; fc < 4; ++fc){
            findArea(x + dir[fc][0], y + dir[fc][1]);
        }
     }
}

void findPortal(char letter, int &x, int &y){
     int fc, fc2;
     for (fc =0; fc < columns; ++fc){
         for (fc2 = 0; fc2 < rows; ++fc2){
             if (grid[fc2][fc] == letter){
                x = fc;
                y = fc2;
                break;
             }
         }
     }
}

bool checkNotSame(int room, char letter){
     list <char>::iterator fit;
     for (fit = adj[room].begin(); fit != adj[room].end(); ++fit){
         if (*fit == letter - 32)
            return false;
     }
     return true;
}

int main(){
    
    ofstream fout("out5.txt");
    ifstream fin ("data5.txt");    
    int input;    
    int c = 0, c2 = 0, c3, c4;
    int xcoord, ycoord;
    

    fin >> rows >> columns;
    
    for (c = 0; c < rows; ++c){
        fin >> grid[c];
    }
    
    findNumCoords();
    match();
    
    for (c = 0; c < 5; ++c){
        reset();
        findArea(coords[0][c],coords[1][c]);
        areas[c] += area;
    }
    
    for (c = 0; c < 5; ++c)
        cout<<areas[c] << endl;
        
    
    
    for (c =0; c< 5; ++c){
        if(adj[c].size() > 0){
                       for (it = adj[c].begin(); it != adj[c].end(); ++it){
                           if (islower(int(*it)) && checkNotSame(c,*it) ){
                              reset();
                              findPortal(*it - 32,xcoord,ycoord);
//                              cout<<"*it is:"<<*it<<" x:"<<xcoord<<" Y:"<<ycoord; cin.get();
                              findArea(xcoord,ycoord);
                              areas[c] += area;
                           }
                       }
        }
    }
    
    for (c = 0; c < 5; ++c)
        fout<<areas[c] << endl;
        
        

    
    
    return 0;
}

