using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static Queue<Tuple<char,int, int>> p = new Queue<Tuple<char,int, int>>();
        static char[,] matrica(int n,int m)
        {
            char[,] c = new char[n, m];
            int v = 0, k = 0;
            for(int i = 0; i < n; i++)
            {
                string s = Console.ReadLine();
                for(int j=0; j < s.Length; j++)
                {
                    c[i, j] = s[j];
                    if (c[i, j] == 'X')
                        p.Enqueue(Tuple.Create('X', i, j));
                    else if (c[i, j] == 'S')
                    {
                        v = i;
                        k = j;
                    }
                }
            }
            p.Enqueue(Tuple.Create('S', v, k));
            return c;
        }
        static int sirenje(char[,] c,int n,int m)
        {
            int br = 0;
            while (p.Count > 0)
            {
                var x_y = p.Dequeue();
                char ch = x_y.Item1;
                int x = x_y.Item2;
                int y = x_y.Item3;
                if (ch == 'S')
                    br++;
                int[] dx = { 0, 1, 0, -1 };
                int[] dy = { 1, 0, -1, 0 };
                for (int i = 0; i < 4; i++)
                {
                    int xx = x + dx[i];
                    int yy = y + dy[i];
                    if (xx >= 0 && xx < n && yy >= 0 && yy < m && c[xx, yy] == '.') 
                    {
                        if (ch == 'X')
                        {
                            p.Enqueue(Tuple.Create('X', xx, yy));
                            c[xx, yy] = 'X';
                        }
                        else
                        {
                            p.Enqueue(Tuple.Create('S', xx, yy));
                            c[xx, yy] = 'S';
                        }
                    }
                }
            }
            return br;
        }
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]);
            int m = int.Parse(s[1]);
            char[,] c = matrica(n, m);
            if (p.Count == 1)
                Console.WriteLine(n * m);
            else
                Console.WriteLine(sirenje(c, n, m));
        }
    }
}