#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;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update>

vector<vector<char>>mat(N, vector<char>(N));

vector<vector<int>>dist(N, vector<int>(N, 1e7));
vector<vector<bool>>vis(N, vector<bool>(N, false));
vector<vector<int>>distv(N, vector<int>(N, 1e7));

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]=='S')
            {
                q.push({i, j});
                dist[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();
        if(vis[x][y])continue;
        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;

            if(dist[a][b]>dist[x][y]+1){
                dist[a][b] = dist[x][y] + 1;
                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]=='X')
            {
                q.push({ i, j });
                distv[i][j]=0;
            }
        }
    }

    while (!q.empty()) {

        int x2 = q.front().first;
        int y2 = q.front().second;
        q.pop();
        if(vis[x2][y2])continue;
        vis[x2][y2]=true;

        for (int i = 0; i < 4; i++) {
            int a2 = x2 + dx[i];
            int b2 = y2 + dy[i];
            if (a2 < 0 || a2 >= n || b2 >= m || b2 < 0)
                continue;
            if(mat[a2][b2]=='S' || mat[a2][b2]=='*')continue;

            if(distv[a2][b2]>distv[x2][y2]+1){
                distv[a2][b2] = distv[x2][y2] + 1;
                q.push({ a2, b2 });
            }

        }
    }
    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";
    while(true);
}

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;
}