global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

namespace _c_pozar_malo_drugacije
{
    internal class Program
    {
        static void OkolnaPolja(int i, int j, int N, int M, ref List<Tuple<int, int>> Polja)
        {
            // gore
            if (i + 1 < N) Polja.Add(new Tuple<int, int>(i + 1, j));
            // dole
            if (i - 1 >= 0) Polja.Add(new Tuple<int, int>(i - 1, j));
            // levo
            if (j - 1 >= 0) Polja.Add(new Tuple<int, int>(i, j - 1));
            // desno
            if (j + 1 < M) Polja.Add(new Tuple<int, int>(i, j + 1));
        }

        static void DostupnaPoljaS(ref List<Tuple<int, int>> Steve,ref string[,] mapa, ref List<Tuple<int,int>> NovaS, ref int brojac, ref bool kraj)
        {
            int N = mapa.GetLength(0);
            int M = mapa.GetLength(1);
            List<Tuple<int, int>> PoljaS = new List<Tuple<int, int>>();

            foreach(var tuple in Steve)
            {
                OkolnaPolja(tuple.Item1, tuple.Item2, N, M, ref PoljaS);
            }

            
            foreach (var tuple in PoljaS)
            {
                int i = tuple.Item1;
                int j = tuple.Item2;
                if (mapa[i,j]==".")
                {
                    NovaS.Add(tuple);
                    mapa[i, j] = "S";
                    brojac++;
                    kraj = false;
                }
            }

            Steve = NovaS;
        }

        static void DostupnaPoljaX(ref List<Tuple<int, int>> Vatre,ref string[,] mapa, ref List<Tuple<int, int>> NovaX, ref int brojac)
        {
            int N = mapa.GetLength(0);
            int M = mapa.GetLength(1);
            List<Tuple<int, int>> PoljaX = new List<Tuple<int, int>>();

            foreach (var tuple in Vatre)
            {
                OkolnaPolja(tuple.Item1, tuple.Item2, N, M, ref PoljaX);
            }

            foreach (var tuple in PoljaX)
            {
                int i = tuple.Item1;
                int j = tuple.Item2;
                if (mapa[i, j] == ".")
                {
                    NovaX.Add(tuple);
                    mapa[i, j] = "X";
                }
                else if (mapa[i,j] == "S")
                {
                    NovaX.Add(tuple);
                    mapa[i, j] = "X";
                    brojac--;
                }
            }
            Vatre = NovaX;

        }

        static bool Prekini(List<Tuple<int, int>> Steve,string[,] mapa)
        {
            int N = mapa.GetLength(0);
            int M = mapa.GetLength(1);
            List<Tuple<int, int>> PoljaS = new List<Tuple<int, int>>();

            foreach (var tuple in Steve)
            {
                OkolnaPolja(tuple.Item1, tuple.Item2, N, M, ref PoljaS);
            }


            foreach (var tuple in PoljaS)
            {
                int i = tuple.Item1;
                int j = tuple.Item2;
                if (mapa[i, j] == ".")
                {
                    return false;
                }
            }
            return true;
        }

        static void Main(string[] args)
        {
            // unos
            string[] ulaz1 = Console.ReadLine().Split(" ");
            int N = int.Parse(ulaz1[0]);
            int M = int.Parse(ulaz1[1]);

            string[,] mapa = new string[N, M];

            for (int i = 0; i < N; i++)
            {
                string ulaz2 = Console.ReadLine();
                for (int j = 0; j < M; j++)
                {
                    mapa[i, j] = Convert.ToString(ulaz2[j]);
                }
            }

            // nadjemo Stevu i vatru

            List<Tuple<int, int>> Steve = new List<Tuple<int, int>>();
            List<Tuple<int, int>> Vatre = new List<Tuple<int, int>>();

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < M; j++)
                {
                    if(mapa[i, j] == "S")
                    {
                        Steve.Add(new Tuple<int, int>(i, j));
                    }
                }
            }

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < M; j++)
                {
                    if (mapa[i, j] == "X")
                    {
                        Vatre.Add(new Tuple<int, int>(i, j));
                    }
                }
            }

            // obradjujemo
            bool kraj = false;
            int brojac = 1;

            List<Tuple<int, int>> NovaS = new List<Tuple<int, int>>();
            List<Tuple<int, int>> NovaX = new List<Tuple<int, int>>();

            while (!kraj)
            {
                kraj = true;
                if (Prekini(Steve,mapa)) break;
                DostupnaPoljaX(ref Vatre, ref mapa, ref NovaX, ref brojac);
                DostupnaPoljaS(ref Steve, ref mapa, ref NovaS, ref brojac, ref kraj);
            }

            Console.WriteLine(brojac);

        }
    }
}