#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")

using namespace std;

#define int long long

void solve(){
    int n, m;
    cin>>n>>m;
    vector<string> v(n);
    for(int i = 0;i<n;i++) cin>>v[i];
    vector<vector<int>> d1(n, vector<int>(m, 1e18));
    vector<vector<int>> d2(n, vector<int>(m, 1e18));
    queue<pair<int, int>> q;
    vector<vector<bool>> b1(n, vector<bool>(m, false));
    for(int i = 0;i<n;i++){
        for(int j = 0;j<m;j++){
            if(v[i][j]=='X'){
                d1[i][j] = 0;
                b1[i][j] = true;
                q.push({i, j});
            }
            if(v[i][j]=='*') b1[i][j] = true;
        }
    }
    vector<int> diri(4);
    vector<int> dirj(4);
    diri = {-1, 0, 1, 0};
    dirj = {0, 1, 0, -1};
    while(!q.empty()){
        int i = q.front().first;
        int j = q.front().second;
        q.pop();
        for(int k = 0;k<4;k++){
            int i2 = i+diri[k], j2 = j+dirj[k];
            if(i2<0) continue;
            if(j2<0) continue;
            if(i2>=n) continue;
            if(j2>=m) continue;
            if(b1[i2][j2]) continue;
            b1[i2][j2] = true;
            d1[i2][j2] = d1[i][j]+1;
            q.push({i2, j2});
        }
    }
    vector<vector<bool>> b2(n, vector<bool>(m, false));
    for(int i = 0;i<n;i++){
        for(int j = 0;j<m;j++){
            if(v[i][j]=='S'){
                d2[i][j] = 0;
                b2[i][j] = true;
                q.push({i, j});
            }
            if(v[i][j]=='*') b2[i][j] = true;
        }
    }
    while(!q.empty()){
        int i = q.front().first;
        int j = q.front().second;
        q.pop();
        for(int k = 0;k<4;k++){
            int i2 = i+diri[k], j2 = j+dirj[k];
            if(i2<0) continue;
            if(j2<0) continue;
            if(i2>=n) continue;
            if(j2>=m) continue;
            if(b2[i2][j2]) continue;
            b2[i2][j2] = true;
            d2[i2][j2] = d2[i][j]+1;
            q.push({i2, j2});
        }
    }
    int cnt = 0;
    for(int i = 0;i<n;i++){
        for(int j = 0;j<m;j++){
            if(v[i][j]=='*') continue;
            if(d1[i][j]<=d2[i][j]) continue;
            cnt++;
        }
    }
    cout<<cnt<<endl;
}

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