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

namespace PozarPokusaj3
{
    internal class Program
    {
        static int Dimcina(char[,] mapa, (int, int) StevoPoc, HashSet<(int, int)> vatrice)
        {
            int red = mapa.GetLength(0);
            int kolona = mapa.GetLength(1);
            (int,int)[] smerovi = new (int, int)[] { (-1, 0), (1, 0), (0, -1), (0, 1) };
            HashSet<(int,int)> posecen = new HashSet<(int, int)>();
            Queue<(int,int)> q = new Queue<(int, int)>();

            q.Enqueue(StevoPoc);

            while (q.Count > 0)
            {
                (int,int) tren = q.Dequeue();
                if (posecen.Contains(tren))
                {
                    continue;
                }
                else
                {
                    posecen.Add(tren);
                }
                
             


                HashSet<(int,int)> VatraSledeca = new HashSet<(int, int)>(vatrice);
                foreach (var vatra in vatrice)
                {
                    foreach (var smer in smerovi)
                    {
                        (int,int) VatraNova = (vatra.Item1 + smer.Item1, vatra.Item2 + smer.Item2);
                        if (VatraNova.Item1 >= 0 && VatraNova.Item1 < red && VatraNova.Item2 >= 0 && VatraNova.Item2 < kolona )
                        {
                            if(mapa[VatraNova.Item1, VatraNova.Item2] != '*')
                            {
                                VatraSledeca.Add(VatraNova);
                            }
                                  
                        }
                    }
                }

                vatrice = VatraSledeca; 

                
                foreach (var smer in smerovi)
                {
                    (int,int) Stevonovi = (tren.Item1 + smer.Item1, tren.Item2 + smer.Item2);
                    if (Stevonovi.Item1 >= 0 && Stevonovi.Item1 < red && Stevonovi.Item2 >= 0 && Stevonovi.Item2 < kolona )
                    {
                        if(mapa[Stevonovi.Item1, Stevonovi.Item2] != '*' && !vatrice.Contains(Stevonovi))
                        {
                            q.Enqueue(Stevonovi);
                        }
                        
                    }
                }
            }

            return posecen.Count;
        }
        static void Main(string[] args)
        {
            string[] ulaz = Console.ReadLine().Split();
            int N = int.Parse(ulaz[0]);
            int M = int.Parse(ulaz[1]);
            char[,] mapa = new char[N, M];
            (int, int) StevoPocetak = (0, 0);
            HashSet<(int, int)> vatrice = new HashSet<(int, int)>();

            for(int i = 0; i < N; i++)
            {
               string ulaz2 = Console.ReadLine();
                for(int j = 0; j < M; j++)
                {
                    mapa[i,j] = ulaz2[j];
                    if(mapa[i,j] == 'S')
                    {
                        StevoPocetak =  (i, j);

                    }
                    else if (mapa[i,j] == 'X')
                    {
                        vatrice.Add((i, j)); 
                    }
                }
            }
            int res = Dimcina(mapa,StevoPocetak,vatrice);
            Console.WriteLine(res);
        }
    }
}