using System;
using System.Collections.Concurrent;

namespace Mamuransam
{
    class Program
    {
        public static long Rezultat(int L,int R, int[]A, int[]B)
        {
            long xor = 0 ^ A[L];
            long and = B[L];
            for(int i = L+1;i<=R;i++)
            {
                xor ^= A[i];
                and &= B[i];
            }
            return xor*and;
        }
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int N = int.Parse(s[0]);
            int M = int.Parse(s[1]);
            string[,] lavirint = new string[N + 2, M + 2];
            string[,] lavirint1 = new string[N + 2, M + 2];
            for (int i = 0; i < N+2; i++)
            {
                lavirint[i, 0] = "*";
                lavirint[i, M+1] = "*";
                lavirint1[i, 0] = "*";
                lavirint1[i, M + 1] = "*";
            }
            for (int i = 0; i < M+2; i++)
            {
                lavirint[0, i] = "*";
                lavirint[N+1, i] = "*";
                lavirint1[0, i] = "*";
                lavirint1[N + 1, i] = "*";
            }
            for(int i = 0;i < N; i++)
            {
                string s1 = Console.ReadLine();
                for(int j = 0; j < M; j++)
                {
                    lavirint[i+1,j+1] = s1[j].ToString();
                    lavirint1[i + 1, j + 1] = s1[j].ToString();
                }
            }
            bool tacka = true;
            while(tacka)
            {
                for(int i = 1 ; i < N + 1; i++)
                    for (int j = 1; j < M + 1; j++)
                    {
                        if (lavirint[i,j]== "X")
                        {
                            if (lavirint[i, j-1] == ".")
                                lavirint1[i,j-1] = "X";
                            if (lavirint[i, j + 1] == ".")
                                lavirint1[i, j + 1] = "X";
                            if (lavirint[i - 1, j] == ".")
                                lavirint1[i - 1, j] = "X";
                            if (lavirint[i + 1, j] == ".")
                                lavirint1[i + 1, j] = "X";
                        }
                    }
                for (int i = 1; i < N + 1; i++)
                    for (int j = 1; j < M + 1; j++)
                    {
                        lavirint[i, j] = lavirint1[i, j];
                    }
                for (int i = 1; i < N + 1; i++)
                    for (int j = 1; j < M + 1; j++)
                    {
                        if (lavirint[i, j] == "S")
                        {
                            if (lavirint[i, j - 1] == ".")
                                lavirint1[i, j - 1] = "S";
                            if (lavirint[i, j + 1] == ".")
                                lavirint1[i, j + 1] = "S";
                            if (lavirint[i - 1, j] == ".")
                                lavirint1[i - 1, j] = "S";
                            if (lavirint[i + 1, j] == ".")
                                lavirint1[i + 1, j] = "S";
                        }
                    }
                for (int i = 1; i < N + 1; i++)
                    for (int j = 1; j < M + 1; j++)
                    {
                        lavirint[i, j] = lavirint1[i, j];
                    }
                tacka = false;
                for (int i = 1; i < N + 1; i++)
                    for (int j = 1; j < M + 1; j++)
                    {
                        if (lavirint[i,j]== ".")
                        {
                            tacka = true;   
                        }    
                    }
                for (int i = 1; i < N + 1; i++)
                {
                    for (int j = 1; j < M + 1; j++)
                    {
                        Console.Write(lavirint[i, j]);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }
            long br = 0;
            for (int i = 1; i < N + 1; i++)
                for (int j = 1; j < M + 1; j++)
                {
                    if (lavirint[i, j] == "S")
                        br++;
                }
            Console.WriteLine(br);
        }
    }
}