using System;

namespace ConsoleApp95
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] n_m = Console.ReadLine().Split();
            int n = int.Parse(n_m[0]); int m = int.Parse(n_m[1]);
            char[,] koord = new char[n, m];
            int[,] vatra = new int[n, m];
            int[,] steva = new int[n, m];
            int[] x = {-1,1,0,0}; int[] y = {0,0,-1,1}; 
            bool status = true; bool status2 = true;
            int izlaz = 0;
            for (int i = 0; i < n; i++)
            {
                string ulaz = Console.ReadLine();
                for (int j = 0; j < m; j++)
                {
                    koord[i,j] = ulaz[j];
                    vatra[i,j] = int.MaxValue;
                    steva[i,j] = int.MaxValue;
                    if (koord[i,j] == 'X')
                    {
                        vatra[i,j] = 0;
                    }
                    else if (koord[i,j] == 'S')
                    {
                        steva[i,j] = 0;
                    }
                }
            }
            while (status)
            {
                status = false;
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        if (vatra[i,j] != int.MaxValue)
                        {
                            for (int d = 0; d < 4; d++)
                            {
                                int x2 = i + x[d];
                                int y2 = j + y[d];
                                if (x2 >= 0 && x2 < n && y2 >= 0 && y2 < m && koord[x2,y2] == '.' && vatra[x2,y2] > vatra[i,j] + 1)
                                {
                                    vatra[x2,y2] = vatra[i,j] + 1;
                                    status = true;
                                }
                            }
                        }
                    }
                }
            }
            while (status2)
            {
                status2 = false;
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        if (steva[i, j] != int.MaxValue)
                        {
                            for (int d = 0; d < 4; d++)
                            {
                                int x2 = i + x[d];
                                int y2 = j + y[d];
                                if (x2 >= 0 && x2 < n && y2 >= 0 && y2 < m && koord[x2,y2] == '.' && steva[x2,y2] > steva[i,j] + 1 && steva[i,j] + 1 < vatra[x2,y2])
                                {
                                    steva[x2,y2] = steva[i,j] + 1;
                                    status2 = true;
                                }
                            }
                        }
                    }
                }
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (steva[i,j] < vatra[i,j])
                    {
                        izlaz++;
                    }
                }
            }
            Console.WriteLine(izlaz);
        }
    }
}