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

namespace Pozar___Petlja_2024
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = 0, M = 0;
            string unos = Console.ReadLine();

            for (int i = 0; i < unos.Length; i++)
                if (unos[i] == ' ')
                {
                    N = Convert.ToInt32(unos.Substring(0, i));
                    M = Convert.ToInt32(unos.Substring(i + 1, unos.Length - 1 - i));
                }

            char[,] mat = new char[N, M];

            for (int i = 0; i < N; i++)
            {
                unos = Console.ReadLine();
                for (int j = 0; j < M; j++)
                    mat[i, j] = unos[j];
            }

            bool sim = true; int br;

            while (sim == true)
                for (int k = 0; k <= 2; k++)
                    for (int i = 0; i < N; i++)
                        for (int j = 0; j < M; j++)
                            switch (k)
                            {
                                case 0:
                                    if (mat[i, j] == 'X')
                                    {
                                        if (i > 0 && mat[i - 1, j] == '.') mat[i - 1, j] = 'x';
                                        if (i < N - 1 && mat[i + 1, j] == '.') mat[i + 1, j] = 'x';
                                        if (j > 0 && mat[i, j - 1] == '.') mat[i, j - 1] = 'x';
                                        if (j < M - 1 && mat[i, j + 1] == '.') mat[i, j + 1] = 'x';
                                    }
                                    break;
                                case 1:
                                    if (mat[i, j] == 'S')
                                    {
                                        if (i > 0 && mat[i - 1, j] == '.') mat[i - 1, j] = 's';
                                        if (i < N - 1 && mat[i + 1, j] == '.') mat[i + 1, j] = 's';
                                        if (j > 0 && mat[i, j - 1] == '.') mat[i, j - 1] = 's';
                                        if (j < M - 1 && mat[i, j + 1] == '.') mat[i, j + 1] = 's';
                                    }
                                    break;
                                case 2:
                                    br = 0;
                                    for (int l = 0; l < N; l++)
                                        for (int o = 0; o < M; o++)
                                        {
                                            if (mat[l, o] == '.') br++;
                                            else if (mat[l, o] == 's') mat[l, o] = 'S';
                                            else if (mat[l, o] == 'x') mat[l, o] = 'X';
                                        }
                                    if (br == 0) sim = false;
                                    break;
                            }

            br = 0;

            for (int i = 0; i < N; i++)
                for (int j = 0; j < M; j++)
                    if (mat[i, j] == 'S') br++;
                
            Console.WriteLine(br);

            Console.ReadLine();
        }
    }
}