using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace vezba
{
    class Program
    {
        static void Vatra(char[,]matrica,int[,]Vvatra, Queue<Tuple<int, int>> vatra)
        {
            int n = matrica.GetLength(0);
            int m = matrica.GetLength(1);
            
            while (vatra.Count > 0)
            {
                var p = vatra.Dequeue();
                int v = p.Item1;
                int k = p.Item2;
                int[] dx = { -1, 0, 1, 0 };
                int[] dy = { 0, 1, 0, -1 };
                for (int i = 0; i < 4; i++)
                {
                    int xx = v + dx[i], yy = k + dy[i];
                    if (xx >= 0 && xx < n && yy >= 0 && yy < m && matrica[xx, yy] == '.' && Vvatra[xx, yy] == 1001)
                    {
                        Vvatra[xx, yy] = Vvatra[v, k] + 1;
                        vatra.Enqueue(Tuple.Create(xx, yy));
                    }
                }
            }
        }
        static int Steva(char[,] matrica, int[,] Vvatra,int[,]Vsteva, Tuple<int, int> stevat)
        {
            int n = matrica.GetLength(0);
            int m = matrica.GetLength(1);
            Queue<Tuple<int, int>> steva = new Queue<Tuple<int, int>>();
            steva.Enqueue(stevat);
            Vsteva[stevat.Item1, stevat.Item2] = 0;
            int br = 0;
            while (steva.Count > 0)
            {
                var p = steva.Dequeue();
                int v = p.Item1;
                int k = p.Item2;
                int[] dx = { -1, 0, 1, 0 };
                int[] dy = { 0, 1, 0, -1 };
                if (Vsteva[v,k] < Vvatra[v, k])
                {
                    br++;
                }
                for (int i = 0; i < 4; i++)
                {
                    int xx = v + dx[i], yy = k + dy[i];
                    if (xx >= 0 && xx < n && yy >= 0 && yy < m && matrica[xx, yy] == '.' && Vsteva[xx, yy] == 1001)
                    {
                        Vsteva[xx, yy] = Vsteva[v, k] + 1;
                        steva.Enqueue(Tuple.Create(xx, yy));
                    }
                }
            }
            return br;
        }
        static void Main(string[] args)
        {
            string[] dim = Console.ReadLine().Split();
            int n = int.Parse(dim[0]);
            int m = int.Parse(dim[1]);
            char[,] matrica = new char[n, m];
            int[,] Vvatra = new int[n, m];
            int[,] Vsteva = new int[n, m];
            Queue<Tuple<int, int>> vatra = new Queue<Tuple<int, int>>();
            Tuple<int, int> stevat = null;
            bool x = false;
            for (int i = 0; i < n; i++)
            {
                string s = Console.ReadLine();
                for (int j = 0; j < m; j++)
                {
                    matrica[i, j] = s[j];
                    Vvatra[i, j] = 1001;
                    Vsteva[i, j] = 1001;
                    if (s[j] == 'X')
                    {
                        vatra.Enqueue(Tuple.Create(i, j));
                         x = true;
                    }
                    if (s[j] == 'S')
                    {
                        stevat=Tuple.Create(i, j);
                    }
                }
            }
            if (x)
            {
                Vatra(matrica, Vvatra, vatra);
            }
            int br = Steva(matrica, Vvatra, Vsteva, stevat);
            Console.WriteLine(br);
            Console.ReadLine();
        }
    }
}