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

namespace ConsoleApp2
{
    class Program
    {
        

        static void Main(string[] args)
        {
            int[] dx = { -1, 1, 0, 0 }; // pomaci za cetiri smera (gore, dole, levo, desno)
            int[] dy = { 0, 0, -1, 1 };
            string[] prvalinija = Console.ReadLine().Split();
            int N = int.Parse(prvalinija[0]);
            int M = int.Parse(prvalinija[1]);
            char[,] matrica = new char[N, M];
            Queue<(int, int)> vatra = new Queue<(int, int)>();
            Queue<(int, int)> stevalegenda = new Queue<(int, int)>();
            int[,] vatrav = new int[N, M];
            int[,] stevav = new int[N, M];
            for (int i = 0; i < N; i++)
            {
                string n = Console.ReadLine();
                for (int j = 0; j < M; j++)
                {
                    matrica[i, j] = n[j];
                    vatrav[i, j] = int.MaxValue;
                    stevav[i, j] = int.MaxValue;
                    if (n[j] == 'X')
                    {
                        vatra.Enqueue((i, j));
                        vatrav[i, j] = 0;
                    }
                    else if (n[j] == 'S')
                    {
                        stevalegenda.Enqueue((i, j));
                        stevav[i, j] = 0;
                    }
                }
            }
            while (vatra.Count > 0)
            {
                var (x, y) = vatra.Dequeue();
                for (int i = 0; i < 4; i++)
                {
                    int nx = x + dx[i], ny = y + dy[i];
                    if (nx >= 0 && nx < N && ny >= 0 && ny < M && matrica[nx, ny] != '*' && vatrav[nx, ny] == int.MaxValue)
                    {
                        vatrav[nx, ny] = vatrav[x, y] + 1;
                        vatra.Enqueue((nx, ny));
                    }
                }
            }
            while (stevalegenda.Count > 0)
            {
                var (x, y) = stevalegenda.Dequeue();
                for (int i = 0; i < 4; i++)
                {
                    int nx = x + dx[i], ny = y + dy[i];
                    if (nx >= 0 && nx < N && ny >= 0 && ny < M && matrica[nx, ny] != '*' && stevav[nx, ny] == int.MaxValue)
                    {
                        if (stevav[x, y] + 1 < vatrav[nx, ny])
                        {
                            stevav[nx, ny] = stevav[x, y] + 1;
                            stevalegenda.Enqueue((nx, ny));
                        }
                    }
                }
            }
            int ispis = 0;
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < M; j++)
                {
                    if (stevav[i, j] < vatrav[i, j])
                    {
                        ispis++;
                    }
                }
            }
            Console.WriteLine(ispis);
            Console.ReadLine();


        }
    }
}