using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;

namespace zadatak
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split();
            int n = int.Parse(input[0]);
            int m = int.Parse(input[1]);
            char[,] matrica = new char[n, m];

            LinkedList<int[]> red = new LinkedList<int[]>();

            int br = 1;
            int xs = 0, ys = 0;
            for (int i = 0; i < n; i++)
            {
                string s = Console.ReadLine();
                for (int j = 0; j < m; j++)
                {
                    matrica[i, j] = s[j];
                    if (matrica[i, j] == 'S')
                    {
                        xs = i;
                        ys = j;
                    }
                    if (matrica[i, j] == 'X')
                        red.AddFirst(new int[] { i, j });
                }
            }
            red.AddFirst(new int[] { xs, ys });
            while (red.Count > 0)
            {
                int[] poz = red.Last();

                int x = poz[0];
                int y = poz[1];

                red.RemoveLast();

                char c = matrica[x, y];
                if (x - 1 >= 0 && matrica[x - 1, y] == '.')
                {
                    matrica[x - 1, y] = c;
                    if (c == 'S')
                        br++;
                    red.AddFirst(new int[] { x - 1, y });
                }
                if (x + 1 < n && matrica[x + 1, y] == '.')
                {
                    matrica[x + 1, y] = c;
                    if (c == 'S')
                        br++;
                    red.AddFirst(new int[] { x + 1, y });
                }
                if (y - 1 >= 0 && matrica[x, y - 1] == '.')
                {
                    matrica[x, y - 1] = c;
                    if (c == 'S')
                        br++;
                    red.AddFirst(new int[] { x, y - 1 });
                }
                if (y + 1 < m && matrica[x, y + 1] == '.')
                {
                    matrica[x, y + 1] = c;
                    if (c == 'S')
                        br++;
                    red.AddFirst(new int[] { x, y + 1 });
                }

            }

            Console.WriteLine(br);
        }
    }
}