#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int INF=1e6+1;
pair <int, int> kretanje[]={{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector <vector <int>> vatrabfs (vector <vector <char>> &mat)
{
    int n=mat.size(), m=mat[0].size();
    vector <vector <int>> dist(n, vector <int> (m, INF));
    queue <pair<int, int>> kju;
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<m; j++)
        {
            if (mat[i][j]=='X')
            {
                kju.push({i, j});
                dist[i][j]=0;
            }
        }
    }
    while (!kju.empty())
    {
        int x=kju.front().first, y=kju.front().second;
        kju.pop();
        for (int i=0; i<4; i++)
        {
            int tx=x+kretanje[i].first, ty=y+kretanje[i].second;
            if (tx<n && ty<m && tx>=0 && ty>=0 && dist[tx][ty]==INF && mat[tx][ty]!='*')
            {
                dist[tx][ty]=dist[x][y]+1;
                kju.push({tx, ty});
            }
        }
    }
    return dist;
}
vector <vector <int>> stevabfs (vector <vector <char>> &mat, int xs, int ys)
{
    int n=mat.size(), m=mat[0].size();
    vector <vector <int>> dist(n, vector <int> (m, INF));
    queue <pair <int, int>> kju;
    dist[xs][ys]=0;
    kju.push({xs, ys});
    while (!kju.empty())
    {
        int x=kju.front().first, y=kju.front().second;
        kju.pop();
        for (int i=0; i<4; i++)
        {
            int tx=x+kretanje[i].first, ty=y+kretanje[i].second;
            if (tx<n && ty<m && tx>=0 && ty>=0 && dist[tx][ty]==INF && mat[tx][ty]!='*')
            {
                dist[tx][ty]=dist[x][y]+1;
                kju.push({tx, ty});
            }
        }
    }
    return dist;
}
int main()
{
    int n, m;
    cin>>n>>m;
    int xs, ys;
    vector <vector <char>> mat(n, vector <char> (m));
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<m; j++)
        {
            char x; cin>>x;
            mat[i][j]=x;
            if (x=='S')
            {
                 xs=i; ys=j;
            }
        }
    }
    vector <vector <int>> dist1=vatrabfs(mat), dist2=stevabfs(mat, xs, ys);
    /*for (int i=0; i<n; i++)
    {
        for (int j=0; j<m; j++) cout<<dist1[i][j]<<" ";
        cout<<endl;
    }
    cout<<endl;
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<m; j++) cout<<dist2[i][j]<<" ";
        cout<<endl;
    }
    cout<<endl;*/
    int cnt=0;
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<m; j++)
        {
            if (dist2[i][j]<dist1[i][j]) cnt++;
        }
    }
    cout<<cnt<<endl;
    return 0;
}