#include <bits/stdc++.h>

using namespace std;

#define MAXN 1001

int n,m;
char a[MAXN][MAXN];
int dist[2][MAXN][MAXN];
queue<pair<int,int>> bfsq;
pair<int,int> dir[4];

bool check(int br,int x,int y)
{
    if (x>=1 and x<=n and y>=1 and y<=m and a[x][y]!='*' and dist[br][x][y]==INT_MAX) return true;
    return false;
}

int main()
{
    dir[0]={1,0},dir[1]={0,1},dir[2]={-1,0},dir[3]={0,-1};
    cin>>n>>m;
    int sx=-1,sy=-1;
    for (int i=1;i<=n;i++)
    {
        string s;cin>>s;
        for (int j=1;j<=m;j++)
        {
            a[i][j]=s[j-1];
            dist[0][i][j]=INT_MAX,dist[1][i][j]=INT_MAX;
            if (a[i][j]=='S') {sx=i;sy=j;dist[1][i][j]=0;}
            if (a[i][j]=='X') {dist[0][i][j]=0;bfsq.push({i,j});}
        }
    }
    while (!bfsq.empty())
    {
        int nodex=bfsq.front().first,nodey=bfsq.front().second;bfsq.pop();
        for (int i=0;i<4;i++)
        {
            int sledx=nodex+dir[i].first,sledy=nodey+dir[i].second;
            if (check(0,sledx,sledy)) {dist[0][sledx][sledy]=dist[0][nodex][nodey]+1;bfsq.push({sledx,sledy});}
        }
    }
    bfsq.push({sx,sy});
    while (!bfsq.empty())
    {
        int nodex=bfsq.front().first,nodey=bfsq.front().second;bfsq.pop();
        for (int i=0;i<4;i++)
        {
            int sledx=nodex+dir[i].first,sledy=nodey+dir[i].second;
            if (check(1,sledx,sledy)) {dist[1][sledx][sledy]=dist[1][nodex][nodey]+1;bfsq.push({sledx,sledy});}
        }
    }
    int ans=0;
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            if (a[i][j]=='*') continue;
            if (dist[1][i][j]<dist[0][i][j]) ans++;
        }
    }
    cout<<ans<<endl;
}