using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;

namespace zadatak
{
    internal class Program
    {
        static char[,] Prosiri_Karakter(char[,] matrica, int n, int m, int x, int y, char c)
        {
            char[,] novaMatrica = matrica;

            if (x + 1 < n && matrica[x + 1, y] == '.')
                novaMatrica[x + 1, y] = c;
            if (x - 1 >= 0 && matrica[x - 1, y] == '.')
                novaMatrica[x - 1, y] = c;
            if (y + 1 < m && matrica[x, y + 1] == '.')
                novaMatrica[x, y + 1] = c;
            if (y - 1 >= 0 && matrica[x, y - 1] == '.')
                novaMatrica[x, y - 1] = c;

            return novaMatrica;
        }
        static int Prebroj(char[,] matrica, int n, int m, char c)
        {
            int br = 0;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    if (matrica[i, j] == c)
                        br++;
                
            return br;
        }
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split();
            int n = int.Parse(input[0]);
            int m = int.Parse(input[1]);
            char[,] matrica = new char[n, m];
            bool stop = false;

            for (int i = 0; i < n; i++)
            {
                string red = Console.ReadLine();
                for (int j = 0; j < m; j++) 
                {
                    matrica[i, j] = red[j];
                }
            }

            while (!stop)
            {
                char[,] novaM = new char[n,m];
                novaM = (char[,])matrica.Clone();
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < m; j++)
                        if (matrica[i, j] == 'X')
                            novaM = Prosiri_Karakter(novaM, n, m, i, j, 'X');
                matrica = novaM;
                novaM = (char[,])matrica.Clone();
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < m; j++)
                        if (matrica[i, j] == 'S')
                            novaM = Prosiri_Karakter(novaM, n, m, i, j, 'S');
                stop = true;
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < m; j++)
                        if (matrica[i, j] == '.')
                            stop = false;
                matrica = novaM;
            }
            Console.WriteLine(Prebroj(matrica, n, m, 'S'));

        }
    }
}