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

namespace ConsoleApp11
{
    internal class Program
    {
       
        static void Main(string[] args)
        {
            int n, m, br = 0, brPolja = 0;
            string[] ulaz = Console.ReadLine().Split(' ');
            string ulaz2;
            bool polje = false;
            n = int.Parse(ulaz[0]);
            m = int.Parse(ulaz[1]);
            char[,] lavirint = new char[n, m];
            (int x, int y) steva = (0, 0);
            (int x, int y)[] vatra = new (int x, int y)[n * m];
            for (int i = 0; i < n; i++)
            {
                ulaz2 = Console.ReadLine();
                for (int j = 0; j < m; j++)
                {
                    lavirint[i, j] = ulaz2[j];
                    if (lavirint[i, j] == 'S')
                    {
                        steva = (i, j);
                    }
                    else if (lavirint[i, j] == 'X')
                    {
                        vatra[br] = (i, j);
                        br++;
                    }
                }
            }
            Array.Resize(ref vatra, br);
            
            for (int j = 0; j < n; j++)
            {
                for (int k = 0; k < m; k++)
                {

                    if (lavirint[j, k] == '.')
                    {
                        
                        for (int i = 0; i < vatra.Length; i++)
                        {
                            
                            if (Math.Abs(steva.x - j) + Math.Abs(steva.y - k) < Math.Abs(vatra[i].x - j) + Math.Abs(vatra[i].y - k))
                            {
                                polje = true;
                            }
                            else
                            {
                                polje = false;
                                break;
                            }
                            
                        }
                        if (polje)
                        {
                            brPolja++;
                        }
                    }
                    else if (lavirint[j, k] == 'S')
                    {
                        brPolja++;
                    }
                }
                   
            }
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine(brPolja);

        }
    }
}