#include <bits/stdc++.h>
using namespace std;
#define STOP 1000000000
#define Nmax 1000000005
int n,m;
vector<string> graf;
int setnjax[]={-1,1,0,0};
int setnjay[]={0,0,-1,1};
vector<vector<int>> vatrabroj, radbroj;
long long aps(long long x)
{    return x<0?-x:x;
}
bool unutra(int x, int y)
{  bool u=false;
if (y>=0 and y<m and x>=0 and x<n and graf[x][y]!='*')
    u=true;
return u;
}
queue<pair<int, int>> vatra;
vector<vector<int>> obidji(vector<string>& g)
{
vector<vector<int>> rez(n,vector<int>(m,0));
for(int i=0;i<n;i++)
{for (int j=0;j<m;j++)
{
    if (g[i][j]!='*')
    {
        for (int ind=0;ind<4;ind++)
        {
        int ni=i+setnjax[ind];
    int nj=j+setnjay[ind];
                if (unutra(ni,nj)) rez[i][j]++;
                }
            }
        }
    }
    return rez;
}
queue<pair<int, int>> rad;
void obidji2(queue<pair<int, int>>&f,vector<vector<int>>& rez) {
    int x,y,pomx,pomy,ind;
pair<int, int> pom;
 while (!f.empty()) {
    pom= f.front();
x=pom.first; y=pom.second;
//cout<<x<<y<<endl;
f.pop();
    for (ind=0;ind<4;ind++) {
        pomx=x+setnjax[ind];
    pomy=y+setnjay[ind];
   //cout<<pomx<<pomy<<endl;
        if (unutra(pomx,pomy) && rez[pomx][pomy]==STOP)
    {  int brojpom=rez[x][y];
    //brojpom++;
      //rez[pomx][pomy]=brojpom;
        rez[pomx][pomy]=brojpom+1;
      // brojpom--;
     //cout<<brojpom<<endl;
    f.emplace(pomx,pomy);
            }
        }
    }
}

int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >>n>>m;
int kraj=0,rez=0,i,j;
graf.resize(n);
pair<int, int> stevaPos;

vatrabroj.assign(n, vector<int>(m, STOP));
radbroj.assign(n, vector<int>(m, STOP));


    for (int i=0;i<n;i++)
{
cin >> graf[i];
kraj++;
        for (int j=0; j<m;j++)
        {
        if (graf[i][j]=='X') {
                vatra.emplace(i,j);
            vatrabroj[i][j] = 0;
            } else if (graf[i][j]=='S')
        {
            rad.emplace(i,j);
    stevaPos={i,j};
        radbroj[i][j]=0;
            }
     }
}
//krah++;
//obidji(vatra, vatrabroj);
//cout<<kraj<<endl;
obidji2(vatra,vatrabroj);
//cout<<kraj<<endl;
kraj++;
obidji2(rad,radbroj);
if(kraj==0) kraj=n+m;
//cout<<kraj<<endl;

    for (i=0;i<n;i++)
{
      for (j=0;j<m;j++)
    {
    if (graf[i][j]=='.' or graf[i][j]=='S')
    //{
        //if (kraj==n+m) kraj=0;
            //cout<<kraj<<rez<<endl;
                if (radbroj[i][j]<vatrabroj[i][j]) rez++;
            //cout<<kraj<<rez<<endl;
    //}
        }
    }
//cout<<kraj<<endl;
cout<<rez<<endl;
return 0;
}