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

namespace Pozar
{
    internal class Program
    {
        static bool U_matrici(int n,int m,int i,int j)
        {
            if(i>=0 && i<n && j>=0 && j<m)
                return true;
            return false;
        }
        static void Pozar(char[,] p,char[,] a,int x,int y, ref int br)
        {
            int n = a.GetLength(0), m = a.GetLength(1);
            char[,] b = new char[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 (U_matrici(n, m, i - 1, j) && b[i - 1, j] != '*')
                            b[i - 1, j] = 'X';
                        if (U_matrici(n, m, i, j + 1) && b[i, j + 1] != '*')
                            b[i, j + 1] = 'X';
                        if (U_matrici(n, m, i + 1, j) && b[i + 1, j] != '*')
                            b[i + 1, j] = 'X';
                        if (U_matrici(n, m, i, j - 1) && b[i, j - 1] != '*')
                            b[i, j - 1] = 'X';
                    }
                }
            }
            if (b[x, y] != 'X')
            {
                int br1 = 0, br2 = 0, br3 = 0, br4 = 0;
                if (U_matrici(n, m, x - 1, y) && p[x - 1, y] == '.')
                {
                    br1++;
                    b[x - 1, y] = 'S';
                    p[x - 1, y] = 'S';
                    a[x - 1, y] = 'S';
                }
                if (U_matrici(n, m, x, y + 1) && p[x, y + 1] == '.')
                {
                    br2++;
                    b[x, y + 1] = 'S';
                    p[x, y + 1] = 'S';
                    a[x, y + 1] = 'S';
                }
                if (U_matrici(n, m, x + 1, y) && p[x + 1, y] == '.')
                {
                    br3++;
                    b[x + 1, y] = 'S';
                    p[x + 1, y] = 'S';
                    a[x + 1, y] = 'S';
                }
                if (U_matrici(n, m, x, y - 1) && p[x, y - 1] == '.')
                {
                    br4++;
                    b[x, y - 1] = 'S';
                    p[x, y - 1] = 'S';
                    a[x, y - 1] = 'S';
                }
                br += br1 + br2 + br3 + br4;
                if (U_matrici(n, m, x - 1, y) && br1>0 && p[x - 1, y] == 'S')
                {
                    Pozar(p, b, x - 1, y, ref br);
                }
                if (U_matrici(n, m, x, y + 1) && br2>0 && p[x, y + 1] == 'S')
                {
                    Pozar(p, b, x, y + 1, ref br);
                }
                if (U_matrici(n, m, x + 1, y) && br3> 0 && p[x + 1, y] == 'S')
                {
                    Pozar(p, b, x + 1, y, ref br);
                }
                if (U_matrici(n, m, x, y - 1) && br4> 0 && p[x, y - 1] == 'S')
                {
                    Pozar(p, b, x, y - 1, ref br);
                }
            }
            else
                br--;
        }
        static void Pocetak(char[,] p, int x, int y, ref int br)
        {
            int n = p.GetLength(0), m = p.GetLength(1);
            int br1 = 0, br2 = 0, br3 = 0, br4 = 0;
            if (U_matrici(n, m, x - 1, y) && p[x - 1, y] == '.')
            {
                p[x - 1, y] = 'S';
                br1++;
            }
            if (U_matrici(n, m, x, y + 1) && p[x, y + 1] == '.')
            {
                p[x, y + 1] = 'S';
                br2++;
            }
            if (U_matrici(n, m, x + 1, y) && p[x + 1, y] == '.')
            {
                p[x + 1, y] = 'S';
                br3++;
            }
            if (U_matrici(n, m, x, y - 1) && p[x, y - 1] == '.')
            {
                p[x, y - 1] = 'S';
                br4++;
            }
            br += br1 + br2 + br3 + br4;
            if (U_matrici(n, m, x - 1, y) && br1 > 0 && p[x - 1, y] == 'S' )
            {
                Pozar(p, p, x - 1, y, ref br);
            }
            if (U_matrici(n, m, x, y + 1) && br2 > 0 && p[x, y + 1] == 'S' )
            {
                Pozar(p, p, x, y + 1, ref br);
            }
            if (U_matrici(n, m, x + 1, y) && br3 > 0 && p[x + 1, y] == 'S')
            {
                Pozar(p, p, x + 1, y, ref br);
            }
            if (U_matrici(n, m, x, y - 1) && br4 > 0 && p[x, y - 1] == 'S' )
            {
                Pozar(p, p, x, y - 1, ref br);
            }
        }
        static void Main(string[] args)
        {
            string[] s=Console.ReadLine().Split(' ');
            int n = int.Parse(s[0]);
            int m = int.Parse(s[1]);
            char[,] a = new char[n, m];
            int x = -1, y = -1;
            for(int i=0;i < n; i++)
            {
                string s1 = Console.ReadLine();
                for(int j=0;j<m;j++)
                {
                    a[i, j] = s1[j];
                    if (a[i, j] =='S')
                    {
                        x= i;
                        y= j;
                    }
                }
            }
            int br = 1;
            Pocetak(a, x, y, ref br);
            Console.WriteLine(br);
            Console.ReadLine();
        }
    }
}