using System;

class Program
{
    static void Main()
    {
        int N = int.Parse(Console.ReadLine().Split()[0]);
        int M = int.Parse(Console.ReadLine().Split()[1]);

        char[,] lavirint = new char[N, M];
        int[,] vatra = new int[N, M];
        int[,] steva = new int[N, M];

        int sx = -1, sy = -1;

        for (int i = 0; i < N; i++)
        {
            string red = Console.ReadLine();
            for (int j = 0; j < M; j++)
            {
                lavirint[i, j] = red[j];
                if (red[j] == 'S') { sx = i; sy = j; }
                vatra[i, j] = -1;
                steva[i, j] = -1;
            }
        }

        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                if (lavirint[i, j] == 'X')
                {
                    vatra[i, j] = 0;
                }
            }
        }

        bool promena = true;
        while (promena)
        {
            promena = false;
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < M; j++)
                {
                    if (vatra[i, j] != -1)
                    {
                        int vreme = vatra[i, j];
                        if (i > 0 && vatra[i - 1, j] == -1 && lavirint[i - 1, j] == '.')
                        {
                            vatra[i - 1, j] = vreme + 1;
                            promena = true;
                        }
                        if (i < N - 1 && vatra[i + 1, j] == -1 && lavirint[i + 1, j] == '.')
                        {
                            vatra[i + 1, j] = vreme + 1;
                            promena = true;
                        }
                        if (j > 0 && vatra[i, j - 1] == -1 && lavirint[i, j - 1] == '.')
                        {
                            vatra[i, j - 1] = vreme + 1;
                            promena = true;
                        }
                        if (j < M - 1 && vatra[i, j + 1] == -1 && lavirint[i, j + 1] == '.')
                        {
                            vatra[i, j + 1] = vreme + 1;
                            promena = true;
                        }
                    }
                }
            }
        }

        steva[sx, sy] = 0;
        promena = true;
        while (promena)
        {
            promena = false;
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < M; j++)
                {
                    if (steva[i, j] != -1)
                    {
                        int vreme = steva[i, j];
                        if (i > 0 && steva[i - 1, j] == -1 && lavirint[i - 1, j] == '.' && (vatra[i - 1, j] == -1 || vatra[i - 1, j] > vreme + 1))
                        {
                            steva[i - 1, j] = vreme + 1;
                            promena = true;
                        }
                        if (i < N - 1 && steva[i + 1, j] == -1 && lavirint[i + 1, j] == '.' && (vatra[i + 1, j] == -1 || vatra[i + 1, j] > vreme + 1))
                        {
                            steva[i + 1, j] = vreme + 1;
                            promena = true;
                        }
                        if (j > 0 && steva[i, j - 1] == -1 && lavirint[i, j - 1] == '.' && (vatra[i, j - 1] == -1 || vatra[i, j - 1] > vreme + 1))
                        {
                            steva[i, j - 1] = vreme + 1;
                            promena = true;
                        }
                        if (j < M - 1 && steva[i, j + 1] == -1 && lavirint[i, j + 1] == '.' && (vatra[i, j + 1] == -1 || vatra[i, j + 1] > vreme + 1))
                        {
                            steva[i, j + 1] = vreme + 1;
                            promena = true;
                        }
                    }
                }
            }
        }

        int broj = 0;
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                if (steva[i, j] != -1 && (vatra[i, j] == -1 || steva[i, j] < vatra[i, j]))
                {
                    broj++;
                }
            }
        }

        Console.WriteLine(broj);
    }
}