#include <bits/stdc++.h>
#define N 1002
#define MAXK 600005
#define MOD 998244353
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define ll long long
using namespace std;

char mat[N][N];
int dist[N][N], distv[N][N];
bool vis[N][N];

void solve() {

    int n, m;cin>>n>>m;

    queue<pii>q;
    for(int i = 0; i<n; i++)
    {
        string s;cin>>s;
        for(int j = 0; j<m; j++)
        {
            mat[i][j]=s[j];
            if(mat[i][j]=='X')
            {
                q.push({i, j});
                distv[i][j]=0;
            }
        }
    }
    int dx[] = { 0, -1, 0, 1};
    int dy[] = { 1, 0, -1, 0};

    while (!q.empty()) {

        int x = q.front().first;
        int y = q.front().second;
        q.pop();

        vis[x][y]=true;

        for (int i = 0; i < 4; i++) {

            int a = x + dx[i];
            int b = y + dy[i];
            if (a < 0 || a >= n || b >= m || b < 0)
                continue;
            if(mat[a][b]=='S' || mat[a][b]=='*')continue;

            distv[a][b] = distv[x][y] + 1;
            if(!vis[a][b])
                q.push({ a, b });

        }
    }

    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            vis[i][j]=false;
            if(mat[i][j]=='S')
            {
                q.push({ i, j });
                dist[i][j]=0;
            }
        }
    }

    while (!q.empty()) {

        int x = q.front().first;
        int y = q.front().second;
        q.pop();

        vis[x][y]=true;

        for (int i = 0; i < 4; i++) {
            int a = x + dx[i];
            int b = y + dy[i];
            if (a < 0 || a >= n || b >= m || b < 0)
                continue;
            if(mat[a][b]=='X' || mat[a][b]=='*')continue;

            dist[a][b] = dist[x][y] + 1;
            if(!vis[a][b])
                q.push({ a, b });


        }
    }

    int ans=0;

    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            if(dist[i][j]<distv[i][j])ans++;
        }
    }

    cout<<ans<<"\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    //int t;cin>>t;
    int t = 1;
    while (t--) {
        solve();
    }

    return 0;
}