#include <bits/stdc++.h>
#define endl '\n'
//#define int long long

using namespace std;

const long long longlongmax=9223372036854775807;
const int modul=998244353;
const long long mod = 1e9 + 7;


mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

const int N=1009,INF=10000009;
char matr[N][N];
int rez=0;
int steva[N][N],vatra[N][N];
struct za_red{
    int i,j,tr;
    za_red(){}
    za_red(int i,int j,int tr){this->i=i;this->j=j;this->tr=tr;}
};
queue<za_red> red;


int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    //freopen("seating.in","r",stdin);
    //freopen("seating.out","w",stdout);

    int n,m;
    cin >> n >> m;
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            cin >> matr[i][j];
            vatra[i][j]=steva[i][j]=-1;
        }
    }
    for(int i=0; i<=n+1; i++) matr[i][0]=matr[i][m+1]='*';
    for(int j=0; j<=m+1; j++) matr[0][j]=matr[n+1][j]='*';
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(matr[i][j]=='X')
                red.push(za_red(i,j,0));
        }
    }
    while(red.size()){
        int i=red.front().i,j=red.front().j,tr=red.front().tr;
        red.pop();
        if(vatra[i][j]!=-1) continue;
        vatra[i][j]=tr;
        if(matr[i-1][j]!='*') red.push(za_red(i-1,j,tr+1));
        if(matr[i+1][j]!='*') red.push(za_red(i+1,j,tr+1));
        if(matr[i][j-1]!='*') red.push(za_red(i,j-1,tr+1));
        if(matr[i][j+1]!='*') red.push(za_red(i,j+1,tr+1));
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(matr[i][j]=='S')
                red.push(za_red(i,j,0));
        }
    }
    while(red.size()){
        int i=red.front().i,j=red.front().j,tr=red.front().tr;
        red.pop();
        if(steva[i][j]!=-1) continue;
        steva[i][j]=tr;
        if(matr[i-1][j]!='*') red.push(za_red(i-1,j,tr+1));
        if(matr[i+1][j]!='*') red.push(za_red(i+1,j,tr+1));
        if(matr[i][j-1]!='*') red.push(za_red(i,j-1,tr+1));
        if(matr[i][j+1]!='*') red.push(za_red(i,j+1,tr+1));
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(steva[i][j]==-1) steva[i][j]=INF;
            if(vatra[i][j]==-1) vatra[i][j]=INF;
        }
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(steva[i][j]<vatra[i][j])
                rez++;
        }
    }
    cout << rez;


    return 0;
}
/*
    abcde
    01234
    04321 --> 12340
              0   1
    011
    110

1
3 1
1 2 3
3 5

*/