#include <bits/stdc++.h>
#define ll long long int
#define pi pair<int, int>
#define pb push_back

using namespace std;

const int nmax= 1005;

char pocmat[nmax][nmax];
int Sudalj[nmax][nmax];
int Xudalj[nmax][nmax];

bool Visited[nmax][nmax];

struct trojka
{
    int i;
    int j;
    int dis;
};
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin>>n>>m;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            cin>>pocmat[i][j];
            Sudalj[i][j]=INT_MAX;
            Xudalj[i][j]=INT_MAX;
        }
    }

    queue<trojka> q;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(pocmat[i][j]=='S')
            {
                trojka t;
                t.i=i;
                t.j=j;
                t.dis=0;
                q.push(t);
                Visited[i][j]=true;
            }
        }
    }
    while(!q.empty())
    {
        trojka cvor=q.front();
        q.pop();
        int i=cvor.i;
        int j=cvor.j;
        int dis=cvor.dis;
        Sudalj[i][j]=dis;
        trojka sused;
        if(i+1<n && !Visited[i+1][j] && pocmat[i+1][j]!='*')
        {
            sused.i=i+1;
            sused.j=j;
            sused.dis=dis+1;
            Visited[i+1][j]=true;
            q.push(sused);
        }
        if(i-1>=0 && !Visited[i-1][j] && pocmat[i-1][j]!='*')
        {
            sused.i=i-1;
            sused.j=j;
            sused.dis=dis+1;
            Visited[i-1][j]=true;
            q.push(sused);
        }
        if(j+1<m && !Visited[i][j+1] && pocmat[i][j+1]!='*')
        {
            sused.i=i;
            sused.j=j+1;
            sused.dis=dis+1;
            Visited[i][j+1]=true;
            q.push(sused);
        }
        if(j-1>=0 && !Visited[i][j-1] && pocmat[i][j-1]!='*')
        {
            sused.i=i;
            sused.j=j-1;
            sused.dis=dis+1;
            Visited[i][j-1]=true;
            q.push(sused);
        }
    }
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            Visited[i][j]=false;
            if(pocmat[i][j]=='X')
            {
                trojka t;
                t.i=i;
                t.j=j;
                t.dis=0;
                q.push(t);
                Visited[i][j]=true;
            }
        }
    }
   while(!q.empty())
    {
        trojka cvor=q.front();
        q.pop();
        int i=cvor.i;
        int j=cvor.j;
        int dis=cvor.dis;
        Xudalj[i][j]=dis;
        trojka sused;
        if(i+1<n && !Visited[i+1][j] && pocmat[i+1][j]!='*')
        {
            sused.i=i+1;
            sused.j=j;
            sused.dis=dis+1;
            Visited[i+1][j]=true;
            q.push(sused);
        }
        if(i-1>=0 && !Visited[i-1][j] && pocmat[i-1][j]!='*')
        {
            sused.i=i-1;
            sused.j=j;
            sused.dis=dis+1;
            Visited[i-1][j]=true;
            q.push(sused);
        }
        if(j+1<m && !Visited[i][j+1] && pocmat[i][j+1]!='*')
        {
            sused.i=i;
            sused.j=j+1;
            sused.dis=dis+1;
            Visited[i][j+1]=true;
            q.push(sused);
        }
        if(j-1>=0 && !Visited[i][j-1] && pocmat[i][j-1]!='*')
        {
            sused.i=i;
            sused.j=j-1;
            sused.dis=dis+1;
            Visited[i][j-1]=true;
            q.push(sused);
        }
    }
    int resenje=0;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(Sudalj[i][j]<Xudalj[i][j]) {resenje++;}
        }
    }
    cout<<resenje;
    return 0;


}