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

namespace TakmicenjeLavirint3
{
    internal class Program
    {

        static void Main(string[] args)
        {
            string[] q = Console.ReadLine().Split();
            int n = int.Parse(q[0]);
            int m = int.Parse(q[1]);
            char[,] a = new char[n, m];
            int Si = 0, Sj = 0, Xi = 0, Xj = 0;
            bool ul = false;
            for (int i = 0; i < n; i++)
            {
                char[] c = Console.ReadLine().ToCharArray();
                for (int j = 0; j < m; j++)
                {
                    a[i, j] = c[j];
                    if (a[i, j] == 'S')
                    {
                        Si = i; Sj = j;
                    }
                    if (a[i, j] == 'X')
                    {
                        Xi = i; Xj = j;
                        ul = true;
                    }
                }
            }
            Queue<(int, int)> XQ = new Queue<(int, int)>();
            int[] x = { 1, -1, 0, 0 };
            int[] y = { 0, 0, 1, -1 };        
                XQ.Enqueue((Xi, Xj));           
            Queue<(int, int)> SQ = new Queue<(int, int)>();
            SQ.Enqueue((Si, Sj));
            while (XQ.Count > 0 && SQ.Count > 0)
            {
                var (x1, y1) = XQ.Dequeue();
                var (x3, y3) = SQ.Dequeue();
                for (int d = 0; d < 4; d++)
                {                 
                   
                        int x2 = x1 + x[d], y2 = y1 + y[d];
                        if (x2 >= 0 && x2 < n && y2 >= 0 && y2 < m && a[x2, y2] == '.' && a[x2, y2] != 'S')
                        {
                          if(ul==true)
                        {
                            a[x2, y2] = 'X';
                        }                           
                            XQ.Enqueue((x2, y2));
                        }                                   
                    int x4 = x3 + x[d], y4 = y3 + y[d];
                    if (x4 >= 0 && x4 < n && y4 >= 0 && y4 < m && a[x4, y4] == '.' && a[x4, y4] != 'X')
                    {
                        a[x4, y4] = 'S';
                        SQ.Enqueue((x4, y4));
                    }
                }
            }

            int g = 0;
            for (int i = 0; i < n; i++)
            {              
                for (int j = 0; j < m; j++)
                {
                    if (a[i, j] == 'S')
                    {
                       g++;
                    }
                }
            }
            Console.WriteLine(g);
            Console.Read();
        }
    }
}