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

namespace treci
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] u = Console.ReadLine().Split();
            int n = int.Parse(u[0]);
            int m = int.Parse(u[1]);
            string[,] a = new string[n, m];
            for (int i = 0; i < n; i++)
            {
                string u1 = Console.ReadLine();
                for (int j = 0; j < m; j++)
                {
                    a[i, j] = u1[j].ToString();
                }
            }
            bool ok = true;
            while (ok)
            {
                ok = false;
                string[,] b = new string[n, m];
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        b[i, j] = a[i, j];
                    }
                }
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        if (a[i, j] == "X")
                        {
                            if (i + 1 < n && a[i + 1, j] == ".") 
                            {
                                b[i + 1, j] = "X";
                                ok = true;
                            }
                            if (i - 1 >= 0 && a[i - 1, j] == ".") 
                            { 
                                b[i - 1, j] = "X"; 
                                ok = true;
                            }
                            if (j + 1 < m && a[i, j + 1] == ".") 
                            { 
                                b[i, j + 1] = "X"; 
                                ok = true; 
                            }
                            if (j - 1 >= 0 && a[i, j - 1] == ".") 
                            { 
                                b[i, j - 1] = "X"; 
                                ok = true; 
                            }
                        }
                        else if (a[i, j] == "S")
                        {
                            if (i + 1 < n && a[i + 1, j] == ".") 
                            { 
                                b[i + 1, j] = "S"; 
                                ok = true; 
                            }
                            if (i - 1 >= 0 && a[i - 1, j] == ".") 
                            { 
                                b[i - 1, j] = "S"; 
                                ok = true; 
                            }
                            if (j + 1 < m && a[i, j + 1] == ".") 
                            { 
                                b[i, j + 1] = "S"; 
                                ok = true; 
                            }
                            if (j - 1 >= 0 && a[i, j - 1] == ".") 
                            { 
                                b[i, j - 1] = "S"; 
                                ok = true; 
                            }
                        }
                    }
                }
                a = b;
            }
            int br = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (a[i, j] == "S")
                    {
                        br++;
                    }
                }
            }
            Console.WriteLine(br);
        }
    }
}