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

namespace pozar
{
    internal class Program
    {
        static void Main(string[] args)
        {
                string[] s = Console.ReadLine().Split();
                int n = int.Parse(s[0]);
                int k = int.Parse(s[1]);
                if (n <= 0 || k <= 0)
                {
                    Console.WriteLine("Neispravne dimenzije.");
                    return;
                }
                string[,] a = new string[n, k];
                for (int i = 0; i < n; i++)
                {
                    string m = Console.ReadLine();
                    for (int j = 0; j < k; j++)
                    {
                        a[i, j] = m[j].ToString();
                    }
                    if (m.Length != k)
                    {
                        Console.WriteLine("Neispravan broj karaktera.");
                        return;
                    }
                }
                bool ok = true;
                while (ok)
                {
                    ok = false;
                    string[,] s1 = new string[n, k];
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < k; j++)
                        {
                            s1[i, j] = a[i, j];
                        }
                    }
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < k; j++)
                        {
                            if (a[i, j] == "X")
                            {
                                if (i + 1 < n && a[i + 1, j] == ".")
                                {
                                    s1[i + 1, j] = "X";
                                    ok = true;
                                }
                                if (i - 1 >= 0 && a[i - 1, j] == ".")
                                {
                                    s1[i - 1, j] = "X";
                                    ok = true;
                                }
                                if (j + 1 < k && a[i, j + 1] == ".")
                                {
                                    s1[i, j + 1] = "X";
                                    ok = true;
                                }
                                if (j - 1 >= 0 && a[i, j - 1] == ".")
                                {
                                    s1[i, j - 1] = "X";
                                    ok = true;
                                }
                            }
                            else if (a[i, j] == "S")
                            {
                                if (i + 1 < n && a[i + 1, j] == ".")
                                {
                                    s1[i + 1, j] = "S";
                                    ok = true;
                                }
                                if (i - 1 >= 0 && a[i - 1, j] == ".")
                                {
                                    s1[i - 1, j] = "S";
                                    ok = true;
                                }
                                if (j + 1 < k && a[i, j + 1] == ".")
                                {
                                    s1[i, j + 1] = "S";
                                    ok = true;
                                }
                                if (j - 1 >= 0 && a[i, j - 1] == ".")
                                {
                                    s1[i, j - 1] = "S";
                                    ok = true;
                                }
                            }
                        }
                    }
                    a = s1;
                }
                int br = 0;
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < k; j++)
                    {
                        if (a[i, j] == "S")
                        {
                            br++;
                        }
                    }
                }
                Console.WriteLine(br);
            }
        }

    }