#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define PI acos(-1)
#define LSB(i) ((i) & -(i))
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define sc second
#define th third
#define fo fourth
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ldb double
#define INF 1e15
#define MOD 1000000007
#define endl "\n"

#define all(data)       data.begin(),data.end()
#define TYPEMAX(type)   std::numeric_limits<type>::max()
#define TYPEMIN(type)   std::numeric_limits<type>::min()
#define MAXN 1007
char a[MAXN][MAXN];
ll ds[MAXN][MAXN],dx[MAXN][MAXN];
bool vis[MAXN][MAXN],vix[MAXN][MAXN];
vector<pll> mv={{-1,0},{1,0},{0,-1},{0,1}};
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    ll n,m; cin>>n>>m;
    pll s;
    vector<pll> vx;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j]; ds[i][j]=dx[i][j]=INF;
            if(a[i][j]=='S') s={i,j};
            else if(a[i][j]=='X') vx.pb({i,j});
        }
    }
    queue<pll> qs;
    qs.push(s);
    ds[s.fi][s.sc]=0;
    vis[s.fi][s.sc]=true;
    while(!qs.empty())
    {
        pll c=qs.front(); qs.pop();
        for(auto xy:mv)
        {
            pll cc; cc.fi=c.fi+xy.fi; cc.sc=c.sc+xy.sc;
            if(cc.fi<1 || cc.fi>n || cc.sc<1 || cc.sc>m || a[cc.fi][cc.sc]=='*' || vis[cc.fi][cc.sc]) continue;
            qs.push({cc.fi,cc.sc});
            ds[cc.fi][cc.sc]=ds[c.fi][c.sc]+1;
            vis[cc.fi][cc.sc]=true;
        }
    }
    queue<pll> qx;
    for(auto x:vx)
    {
        qx.push(x);
        dx[x.fi][x.sc]=0;
        vix[x.fi][x.sc]=true;
    }
    while(!qx.empty())
    {
        pll c=qx.front(); qx.pop();
        for(auto xy:mv)
        {
            pll cc; cc.fi=c.fi+xy.fi; cc.sc=c.sc+xy.sc;
            if(cc.fi<1 || cc.fi>n || cc.sc<1 || cc.sc>m || a[cc.fi][cc.sc]=='*' || vix[cc.fi][cc.sc]) continue;
            qx.push({cc.fi,cc.sc});
            dx[cc.fi][cc.sc]=dx[c.fi][c.sc]+1;
            vix[cc.fi][cc.sc]=true;
        }
    }
    ll rez=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]=='S' || (a[i][j]=='.' && ds[i][j]<dx[i][j])) rez++;
        }
    }
    cout<<rez<<endl;
    return 0;
}