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

namespace pozar
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string input=Console.ReadLine();
            string[] inputs=input.Split(' ');
            int N = int.Parse(inputs[0]);
            int M = int.Parse(inputs[1]);
            char[,] map = new char[N,M];
            int numOfSpace = N * M;
            int zidic=0;
            bool change = true;
            int sCount = 0;
            int xCount = 0;
            for (int i = 0; i < N; i++) {
                input = Console.ReadLine();
                char[] inputa=input.ToCharArray();
                for (int j = 0; j < M; j++) {
                    if (inputa[j] == '*') { zidic++; }
                    if(inputa[j] == 'S') { sCount++; }
                    if (inputa[j] == 'X') {  xCount++; }
                    map[i, j] =inputa[j];
                }
            }

            while (sCount+xCount +zidic< numOfSpace&&change) {
               change = false;
                for (int i = 0; i < N; i++) {
                    for (int j = 0; j < M; j++) {
                        if (map[i, j] == 'X')
                        {
                            if (i - 1 > -1)
                            {
                                if (map[i - 1, j] != '*'|| map[i - 1, j] != 'X')
                                {
                                    map[i - 1, j] = 'x';
                                    
                                }
                            }
                            if (j - 1 > -1)
                            {
                                if (map[i, j - 1] != '*' || map[i , j-1] != 'X')
                                {
                                    map[i, j - 1] = 'x';
                                    
                                }
                            }
                            if (i + 1 < N)
                            {
                                if (map[i + 1, j] != '*' || map[i + 1, j] != 'X')
                                {
                                    map[i + 1, j] = 'x';
                                    
                                }
                            }
                            if (j + 1 < M)
                            {
                                if (map[i, j + 1] != '*' || map[i , j+1] != 'X')
                                {
                                    map[i, j + 1] = 'x';
                                    
                                }
                            }
                        }
                        if (map[i, j] == 'S') {
                            if (i - 1 > -1)
                            {
                                if (map[i - 1, j] == '.')
                                {
                                    map[i - 1, j] = 's';
                                    
                                }
                            }
                            if (j - 1 > -1) {
                                if (map[i, j - 1] == '.')
                                {
                                    map[i, j - 1] = 's';
                                   
                                }
                            }
                            if (i + 1 < N) {
                                if (map[i + 1, j] == '.')
                                {
                                    map[i + 1, j] = 's';
                                   
                                }
                            }
                            if (j + 1 < M) {
                                if (map[i, j + 1] == '.')
                                {
                                    map[i, j + 1] = 's';
                                    
                                }
                            }
                        }
                    }
                }
                for (int i = 0; i < N; i++) {
                    for (int j = 0; j < M; j++) {
                        
                        if(map[i, j] == 's') { map[i, j] = 'S'; sCount++;change = true; }
                        if (map[i, j] == 'x') { map[i, j] = 'X';xCount++; change = true; }
                        
                    }
                   
                }

               }
            Console.WriteLine(sCount);
        }
    }
}