import java.io.*;
import java.util.*;

// The "Template" class.
public class FloorPlan
{

    static int X, Y;

    public static void printGrid (char[] [] floor, int row, int col)
    {

	for (int i = 0 ; i < row ; i++)
	{

	    //
	    for (int j = 0 ; j < col ; j++)
	    {
		System.out.print (floor [i] [j] + " ");
	    }
	    System.out.println ();

	}


    }


    public static char convertToChar (int room)
    {

	char roomChar;

	switch (room)
	{
	    case 1:
		roomChar = '1';
		break;
	    case 2:
		roomChar = '2';
		break;
	    case 3:
		roomChar = '3';
		break;
	    case 4:
		roomChar = '4';
		break;
	    case 5:
		roomChar = '5';
		break;
	    default:
		roomChar = ' ';
	}

	return roomChar;

    }


    public static void findRoom (int room, char[] [] floor, int row, int col)
    {

	char roomChar = convertToChar (room);

	// Find the room 1 first
	for (int r = 0 ; r < row ; r++)
	{
	    for (int c = 0 ; c < col ; c++)
	    {
		char temp = floor [r] [c];

		if (temp == roomChar)
		{
		    X = r;
		    Y = c;
		    break;
		}

	    }
	}

    }


    public static void main (String[] args) throws IOException
    {

	// Open up the input and output file for IO purpose
	FileReader inFile = new FileReader ("DATA3.txt");
	FileWriter outFile = new FileWriter ("OUT3.txt");

	// Link the input and output file for
	BufferedReader in = new BufferedReader (inFile);
	BufferedWriter out = new BufferedWriter (outFile);

	String line;
	int row, col;

	line = in.readLine ();
	row = Integer.parseInt (line);

	line = in.readLine ();
	col = Integer.parseInt (line);

	// Create 2 D array
	char[] [] floor = new char [row] [col];

	int currentRow = 0;

	// Keep reading as long as not end of file (eof)
	while ((line = in.readLine ()) != null)
	{


	    for (int i = 0 ; i < col ; i++)
	    {
		floor [currentRow] [i] = line.charAt (i);
	    }

	    currentRow++;
	}

	//printGrid (floor, row, col);


	int area = 0;
	boolean lock = false;

	// printGrid(floor, row, col);
	// findRoom (5, floor, row, col);
	// System.out.println("X = " + X);
	// System.out.println("Y = " + Y);
	
	

	for (int room = 1 ; room <= 5 ; room++)
	{

	    //System.out.println ("At room = " + room);

	    if (lock == false)
	    {
		area = 1;
		lock = true;
	    }


	    findRoom (room, floor, row, col);

	    // Calculate UP location
	    int up_x = X - 1;
	    int up_y = Y;

	    if (up_x >= 0 && up_x < row && up_y >= 0 && up_y < col)
	    {

		// check if location is empty space.
		if (floor [up_x] [up_y] == '.')
		{

		    // System.out.println ("Up");
		    // System.out.println ("location = " + up_x + " , " + up_y);

		    area++;
		    floor [X] [Y] = '#';
		    floor [up_x] [up_y] = convertToChar (room);

		    X = up_x;
		    Y = up_y;
		    room--;
		    continue;
		}
	    }

	    // Calculate LEFT location
	    int left_x = X;
	    int left_y = Y - 1;


	    if (left_x >= 0 && left_x < row && left_y >= 0 && left_y < col)
	    {

		// check if location is empty space.
		if (floor [left_x] [left_y] == '.')
		{

		    // System.out.println ("Left");
		    // System.out.println ("location = " + left_x + " , " + left_y);

		    area++;
		    floor [X] [Y] = '#';
		    floor [left_x] [left_y] = convertToChar (room);

		    X = left_x;
		    Y = left_y;
		    room--;
		    continue;
		}
	    }

	    // Calculate DOWN location
	    int down_x = X + 1;
	    int down_y = Y;

	    if (down_x >= 0 && down_x < row && down_y >= 0 && down_y < col)
	    {

		// check if location is empty space.
		if (floor [down_x] [down_y] == '.')
		{
		    // System.out.println ("Down");
		    // System.out.println ("location = " + down_x + " , " + down_y);

		    area++;
		    floor [X] [Y] = '#';
		    floor [down_x] [down_y] = convertToChar (room);

		    X = down_x;
		    Y = down_y;
		    room--;
		    continue;
		}
	    }

	    // Calculate RIGHT location
	    int right_x = X;
	    int right_y = Y + 1;


	    if (right_x >= 0 && right_x < row && right_y >= 0 && right_y < col)
	    {

		// check if location is empty space.
		if (floor [right_x] [right_y] == '.')
		{

		    // System.out.println ("Right");
		    // System.out.println ("location = " + right_x + " , " + right_y);

		    area++;
		    floor [X] [Y] = '#';
		    floor [right_x] [right_y] = convertToChar (room);

		    X = right_x;
		    Y = right_y;
		    room--;
		    continue;
		}
	    }

	    out.write ("" + area);
	    out.newLine ();
	    lock = false;

	   // printGrid (floor, row, col);

	}


	// out.write("There are " + count + " words in the file.");

	// Close input and output file
	in.close ();
	out.close ();

    } // main method
} // End of Class



