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

namespace _2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int i1 = int.Parse(s[0]);
            int j1 = int.Parse(s[1]);
            string[,] a = new string[i1, j1];
            for (int i = 0; i < i1; i++)
            {
                string line = Console.ReadLine();
                for (int j = 0; j < j1; j++)
                {
                    a[i, j] = line[j].ToString();
                }
            }
            bool pr;
            do
            {
                pr = false;
                List<(int, int)> X = new List<(int, int)>();
                for (int i = 0; i < i1; i++)
                {
                    for (int j = 0; j < j1; j++)
                    {
                        if (a[i, j] == "X")
                        {
                            if (i + 1 < i1 && a[i + 1, j] == ".") X.Add((i + 1, j));
                            if (i - 1 >= 0 && a[i - 1, j] == ".") X.Add((i - 1, j));
                            if (j + 1 < j1 && a[i, j + 1] == ".") X.Add((i, j + 1));
                            if (j - 1 >= 0 && a[i, j - 1] == ".") X.Add((i, j - 1));
                        }
                    }
                }
                foreach (var (x, y) in X)
                {
                    if (a[x, y] == ".")
                    {
                        a[x, y] = "X";
                        pr = true;
                    }
                }
                List<(int, int)> S = new List<(int, int)>();
                for (int i = 0; i < i1; i++)
                {
                    for (int j = 0; j < j1; j++)
                    {
                        if (a[i, j] == "S")
                        {
                            if (i + 1 < i1 && a[i + 1, j] == ".") S.Add((i + 1, j));
                            if (i - 1 >= 0 && a[i - 1, j] == ".") S.Add((i - 1, j));
                            if (j + 1 < j1 && a[i, j + 1] == ".") S.Add((i, j + 1));
                            if (j - 1 >= 0 && a[i, j - 1] == ".") S.Add((i, j - 1));
                        }
                    }
                }
                foreach (var (x, y) in S)
                {
                    if (a[x, y] == ".")
                    {
                        a[x, y] = "S";
                        pr = true;
                    }
                }
            }
            while (pr);
            int b = 0;
            for (int i = 0; i < i1; i++)
            {
                for (int j = 0; j < j1; j++)
                {
                    if (a[i, j] == "S")
                    {
                        b++;
                    }
                }
            }
            Console.WriteLine(b);
        }
    }
}