#include <iostream>
#include <deque>
using namespace std;

int main()
{
    int n, m, steve_br=0, vatre_br=0, steve_br_uk=0, x=100;
    bool gotovo=false, promenjeno=true;
    cin>>n>>m;
    char Tabla[n][m];
    deque <pair<int, int> > Steve;
    deque <pair<int, int> > Vatre;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            cin>>Tabla[i][j];
            if(Tabla[i][j]=='S')
            {
                Steve.push_front(make_pair(i, j));
                steve_br++;
                steve_br_uk++;
            }
            else if(Tabla[i][j]=='X')
            {
                Vatre.push_back(make_pair(i, j));
                vatre_br++;
            }
        }
    for(int i=0;i<n*m;i++)
    {
        int pon_s=steve_br, pon_v=vatre_br;
        bool promenjeno=false;
        for(int j=0;j<pon_v;j++)
        {
            int y=Vatre.front().first, x=Vatre.front().second;
            if((y<n-1) && Tabla[y+1][x]=='.')
            {
                Tabla[y+1][x]='X';
                vatre_br++;
                Vatre.push_back(make_pair(y+1, x));
                promenjeno=true;
            }
            if((x<m-1) && Tabla[y][x+1]=='.')
            {
                Tabla[y][x+1]='X';
                vatre_br++;
                Vatre.push_back(make_pair(y, x+1));
                promenjeno=true;
            }
            if((y>0) && Tabla[y-1][x]=='.')
            {
                Tabla[y-1][x]='X';
                vatre_br++;
                Vatre.push_back(make_pair(y-1, x));
                promenjeno=true;
            }
            if((x>0) && Tabla[y][x-1]=='.')
            {
                Tabla[y][x-1]='X';
                vatre_br++;
                Vatre.push_back(make_pair(y, x-1));
                promenjeno=true;
            }
            Vatre.pop_front();
            vatre_br--;
        }
        for(int j=0;j<pon_s;j++)
        {
            int y=Steve.front().first, x=Steve.front().second;
            if((y<n-1) && Tabla[y+1][x]=='.')
            {
                Tabla[y+1][x]='S';
                steve_br++; steve_br_uk++;
                Steve.push_back(make_pair(y+1, x));
                promenjeno=true;
            }
            if((x<m-1) && Tabla[y][x+1]=='.')
            {
                Tabla[y][x+1]='S';
                steve_br++; steve_br_uk++;
                Steve.push_back(make_pair(y, x+1));
                promenjeno=true;
            }
            if((y>0) && Tabla[y-1][x]=='.')
            {
                Tabla[y-1][x]='S';
                steve_br++; steve_br_uk++;
                Steve.push_back(make_pair(y-1, x));
                promenjeno=true;
            }
            if((x>0) && Tabla[y][x-1]=='.')
            {
                Tabla[y][x-1]='S';
                steve_br++; steve_br_uk++;
                Steve.push_back(make_pair(y, x-1));
                promenjeno=true;
            }
            Steve.pop_front();
            steve_br--;
        }
        if(!promenjeno)
        {
            i=n*m;
        }
    }
    cout<<steve_br_uk;
    return 0;
}