#include <bits/stdc++.h>

using namespace std;

#define ll long long
const int INF=1e9;
const ll LINF=1e18,MOD=1e9+7;
#define fi first
#define se second
#define pii pair<int,int>
#define mid ((l+r)/2)
#define sz(a) (int((a).size()))
#define all(a) a.begin(),a.end()
#define endl "\n"
#define pb push_back

void PRINT(int x) {cerr << x;}
void PRINT(ll x) {cerr << x;}
void PRINT(double x) {cerr << x;}
void PRINT(char x) {cerr << '\'' << x << '\'';}
void PRINT(string x) {cerr << '\"' << x << '\"';}
void PRINT(bool x) {cerr << (x ? "true" : "false");}

template<typename T,typename V>
void PRINT(pair<T,V>& x){
    cerr<<"{";
    PRINT(x.fi);
    cerr<<",";
    PRINT(x.se);
    cerr<<"}";
}
template<typename T>
void PRINT(T &x){
    int id=0;
    cerr<<"{";
    for(auto _i:x){
        cerr<<(id++ ? "," : "");
        PRINT(_i);
    }
    cerr<<"}";
}
void _PRINT(){
    cerr<<"]\n";
}
template<typename Head,typename... Tail>
void _PRINT(Head h,Tail... t){
    PRINT(h);
    if(sizeof...(t)) cerr<<", ";
    _PRINT(t...);
}

#define Debug(x...) cerr<<"["<<#x<<"]=["; _PRINT(x)

int n,m;
vector<int> di={+1,0,-1,0};
vector<int> dj={0,+1,0,-1};
char a[1001][1001];
bool used1[1001][1001],used2[1001][1001];
int dv[1001][1001];
int ds[1001][1001];
bool moze[1001][1001];

bool Valid1(int i,int j){
    return (1<=i && i<=n) && (1<=j && j<=m) && (a[i][j]=='.' || a[i][j]=='S');
}

bool Valid2(int i,int j){
    return (1<=i && i<=n) && (1<=j && j<=m) && a[i][j]=='.';
}

void Solve(){
    cin>>n>>m;
    queue<pii> q1;
    queue<pii> q2;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            dv[i][j]=1e9;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
            if(a[i][j]=='X'){
                q1.push({i,j});
                dv[i][j]=0;
                used1[i][j]=true;
            }
            else if(a[i][j]=='S'){
                q2.push({i,j});
                used2[i][j]=true;
            }
        }
    }
    while(!q1.empty()){
        int i=q1.front().fi;
        int j=q1.front().se;
        q1.pop();
        for(int k=0;k<4;k++){
            int i1=i+di[k];
            int j1=j+dj[k];
            if(Valid1(i1,j1) && !used1[i1][j1]){
                used1[i1][j1]=true;
                q1.push({i1,j1});
                dv[i1][j1]=dv[i][j]+1;
            }
        }
    }
    while(!q2.empty()){
        int i=q2.front().fi;
        int j=q2.front().se;
        q2.pop();
        moze[i][j]=true;
        for(int k=0;k<4;k++){
            int i1=i+di[k];
            int j1=j+dj[k];
            if(Valid2(i1,j1) && !used2[i1][j1] && ds[i][j]+1<dv[i1][j1]){
                used2[i1][j1]=true;
                q2.push({i1,j1});
                ds[i1][j1]=ds[i][j]+1;
            }
        }
    }
    int res=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            res+=moze[i][j];
        }
    }
    cout<<res<<endl;
}

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