using System;
using System.Diagnostics.CodeAnalysis;



internal class Program
{
    static void Main(string[] args)
    {
        string s = Console.ReadLine();
        s = s + ' ';
        int t = 0;
        string s1 = "";
        while (!(s[t].Equals(' ')))
        {
            s1 = s1 + s[t];
            t++;
        }
        t++;
        int n = Convert.ToInt32(s1);
        s1 = "";
        while (!(s[t].Equals(' ')))
        {
            s1 = s1 + s[t];
            t++;
        }
        int m = Convert.ToInt32(s1);
        char[,] c=new char[n+2,m+2];
        int k = 0;
        int l= 0;
        int sum = 0;
        string g = "";
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (j == 1) {  g = Console.ReadLine(); }
                c[i, j] = g[j-1];
                if (c[i, j].Equals('S')) { k = i; l = j; }
                if(c[i, j].Equals('X')) { sum++; }
            }
        }
        
        int u = 1;
        if (sum == 0) { u = m * n; }
        else
        {
            bool b = true;
            while (b)
            {
                b = false;
                for (int i = 1; i <= n; i++)
                {
                    for (int j = 1; j <= m; j++)
                    {
                        if (c[i, j].Equals('X')) 
                        {
                            if (c[i-1,j].Equals('.')) { c[i - 1, j] = 'x' ; }
                            if (c[i + 1, j].Equals('.')) { c[i + 1, j] = 'x'; }
                            if (c[i , j-1].Equals('.')) { c[i, j - 1] = 'x'; }
                            if (c[i , j+1].Equals('.')) { c[i, j + 1] = 'x'; }
                        }
                       
                    }
                }
                for (int i = 1; i <= n; i++)
                {
                    for (int j = 1; j <= m; j++)
                    {
                        if (c[i, j].Equals('x'))
                        {
                            c[i, j] = 'X';
                        }

                    }
                }
                for (int i = 1; i <= n; i++)
                {
                    for (int j = 1; j <= m; j++)
                    {
                       
                        if (c[i, j].Equals('S'))
                        {
                            if (i - 1 > 0) { if (c[i - 1, j].Equals('.')) { c[i - 1, j] = 's'; u++; b = true; } }
                            if (i + 1 <= n)
                            {
                                if (c[i + 1, j].Equals('.')) { c[i + 1, j] = 's'; u++; b = true; }
                            }
                            if (j - 1 > 0)
                            {
                                if (c[i, j - 1].Equals('.')) { c[i, j - 1] = 's'; u++; b = true; }
                            }
                            if (j+ 1 <= m)
                            {
                                if (c[i, j + 1].Equals('.')) { c[i, j + 1] = 's'; u++; b = true; }
                            }
                        }
                    }
                }
                for (int i = 1; i <= n; i++)
                {
                    for (int j = 1; j <= m; j++)
                    {
                        if (c[i, j].Equals('s'))
                        {
                            c[i, j] = 'S';
                        }

                    }
                }
            }
        }
        Console.WriteLine(u);
    }
}