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

namespace Pozar
{
    internal class Program
    {
        static int BFS(char[,] a, int n, int m, int x1, int y1,int x2,int y2)
        {
            int s = 0;
            Queue<Tuple<int, int>> kueue1 = new Queue<Tuple<int, int>>();
            Queue<Tuple<int, int>> kueue2 = new Queue<Tuple<int, int>>();
            kueue2.Enqueue(new Tuple<int, int>(x2, y2));
            int k = 0;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if (a[i,j]=='X')
                    {
                        kueue1.Enqueue(new Tuple<int,int>(i,j));
                        k++;
                    }
                }
            }
            int[,] b = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
            while(kueue2.Count()>0)
            {
                Tuple<int, int> q2 = kueue2.Dequeue();
                for(int j=0;j<k;j++)
                {
                    Tuple<int, int> q1 = kueue1.Dequeue();
                    for (int i = 0; i < 4; i++)
                    {
                        int x11 = b[i, 0] + q1.Item1;
                        int y11 = b[i, 1] + q1.Item2;
                        if (UMatrici(n, m, x11, y11) && (a[x11, y11] == '.' || a[x11, y11] == 'S'))
                        {
                            Tuple<int, int> q11 = new Tuple<int, int>(x11, y11);
                            a[q11.Item1, q11.Item2] = 'X';
                            kueue1.Enqueue(q11);
                        }
                    }
                }
                for (int i = 0; i < 4; i++)
                {
                    int x22 = b[i, 0] + q2.Item1;
                    int y22 = b[i, 1] + q2.Item2;
                    if (UMatrici(n, m, x22, y22) && a[x22, y22] == '.')
                    {
                        Tuple<int, int> q22 = new Tuple<int, int>(x22, y22);
                        a[q22.Item1, q22.Item2] = 'S';
                        s++;
                        kueue2.Enqueue(q22);
                    }
                }
            }
            return s+1;

        }
        static bool UMatrici(int n, int m, int x, int y)
        {
            return x < n && y < m && x >= 0 && y >= 0;
        }
        static void Main(string[] args)
        {
            string[] unos = Console.ReadLine().Split();
            int n = int.Parse(unos[0]), m = int.Parse(unos[1]);
            char[,] a = new char[n, m];
            int x1 = 0, y1 = 0,x2=0,y2=0;
            for (int i = 0; i < n; i++)
            {
                string unos1 = Console.ReadLine();
                for (int j = 0; j < m; j++)
                {
                    a[i, j] = unos1[j];
                    if (a[i, j] == 'S')
                    {
                        x2 = i;
                        y2 = j;
                    }
                }
            }
            Console.WriteLine(BFS(a, n, m, x1, y1,x2,y2));
            Console.ReadLine();
        }
    }
}