#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n, m;
    cin >> n >> m;
    string ulaz[n];
    int steva [n][m], vatra[n][m];

    for(int i = 0; i<n; i++){
        cin >> ulaz[i];
    }

    queue<int> x1;
    queue<int> y1;
    queue<int> z1;

    queue<int> x2;
    queue<int> y2;
    queue<int> z2;



    for(int i = 0; i<n; i++){
        for (int j = 0; j<m; j++){
            //cout << ulaz[i];
            if (ulaz[i][j]=='.'){
                steva[i][j]=0;
                vatra[i][j]=0;
            }
            if (ulaz[i][j]=='*'){
                steva[i][j]=-1;
                vatra[i][j]=-1;
            }
            if(ulaz[i][j]=='S'){
                steva[i][j]=1;
                vatra[i][j]=0;
                x1.push(i);
                y1.push(j);
                z1.push(1);
            }
            if(ulaz[i][j]=='X'){
                steva[i][j]=0;
                vatra[i][j]=1;
                x2.push(i);
                y2.push(j);
                z2.push(1);
            }

        }
    }


    while(!x1.empty()){
        int a=x1.front();
        int b=y1.front();
        int c=z1.front();
        if(a-1>=0 && steva[a-1][b]==0){
            steva[a-1][b]=c+1;
            x1.push(a-1);
            y1.push(b);
            z1.push(c+1);
        }if(b-1>=0 && steva[a][b-1]==0){
            steva[a][b-1]=c+1;
            x1.push(a);
            y1.push(b-1);
            z1.push(c+1);
        }if(a+1<n && steva[a+1][b]==0){
            steva[a+1][b]=c+1;
            x1.push(a+1);
            y1.push(b);
            z1.push(c+1);
        }if(b+1<m && steva[a][b+1]==0){
            steva[a][b+1]=c+1;
            x1.push(a);
            y1.push(b+1);
            z1.push(c+1);
        }

        x1.pop();
        y1.pop();
        z1.pop();
    }
    while(!x2.empty()){
        int a=x2.front();
        int b=y2.front();
        int c=z2.front();
        if(a-1>=0 && vatra[a-1][b]==0){
            vatra[a-1][b]=c+1;
            x2.push(a-1);
            y2.push(b);
            z2.push(c+1);
        }if(b-1>=0 && vatra[a][b-1]==0){
            vatra[a][b-1]=c+1;
            x2.push(a);
            y2.push(b-1);
            z2.push(c+1);
        }if(a+1<n && vatra[a+1][b]==0){
            vatra[a+1][b]=c+1;
            x2.push(a+1);
            y2.push(b);
            z2.push(c+1);
        }if(b+1<m && vatra[a][b+1]==0){
            vatra[a][b+1]=c+1;
            x2.push(a);
            y2.push(b+1);
            z2.push(c+1);
        }
        x2.pop();
        y2.pop();
        z2.pop();
    }


    int br = 0;
    for (int i = 0; i<n; i++){
        for(int j = 0; j<m; j++){
            if (steva[i][j]<vatra[i][j]){
                br++;
            }
        }
    }
    cout << br;


    return 0;
}