#include <bits/stdc++.h>
using namespace std;

int main()
{
    int m, n; cin>>m>>n;
    vector<vector<int>> vS(m, vector<int>(n, -1));
    vector<vector<int>> vX(m, vector<int>(n, -1));
    deque<pair<int,int>> dqS;
    deque<pair<int,int>> dqX;

    bool fire=false;

    for(int i=0; i<m; i++)
    {
        string s; cin>>s;
        for(int j=0; j<n; j++)
        {
            if(s[j]=='S')
            {
                dqS.push_front(make_pair(i, j));
                vS[i][j] = 0;
            }
            else if(s[j]=='X')
            {
                dqX.push_front(make_pair(i, j));
                vX[i][j] = 0;
                fire=true;
            }
            else if(s[j]=='*')
            {
                vS[i][j] = -2;
                vX[i][j] = -2;
            }
        }
    }

    while(!dqS.empty())
    {
        pair<int, int> p = dqS.back();
        dqS.pop_back();
        int i=p.first, j=p.second, a=vS[i][j]+1;

        if(i>0 && vS[i-1][j]==-1)
        {
            vS[i-1][j]=a;
            dqS.push_front(make_pair(i-1, j));
        }
        if(i<m-1 && vS[i+1][j]==-1)
        {
            vS[i+1][j]=a;
            dqS.push_front(make_pair(i+1, j));
        }
        if(j>0 && vS[i][j-1]==-1)
        {
            vS[i][j-1]=a;
            dqS.push_front(make_pair(i, j-1));
        }
        if(j<n-1 && vS[i][j+1]==-1)
        {
            vS[i][j+1]=a;
            dqS.push_front(make_pair(i, j+1));
        }
    }

    while(!dqX.empty())
    {
        pair<int, int> p = dqX.back();
        dqX.pop_back();
        int i=p.first, j=p.second, a=vX[i][j]+1;

        if(i>0 && vX[i-1][j]==-1)
        {
            vX[i-1][j]=a;
            dqX.push_front(make_pair(i-1, j));
        }
        if(i<m-1 && vX[i+1][j]==-1)
        {
            vX[i+1][j]=a;
            dqX.push_front(make_pair(i+1, j));
        }
        if(j>0 && vX[i][j-1]==-1)
        {
            vX[i][j-1]=a;
            dqX.push_front(make_pair(i, j-1));
        }
        if(j<n-1 && vX[i][j+1]==-1)
        {
            vX[i][j+1]=a;
            dqX.push_front(make_pair(i, j+1));
        }
    }

    int out=0;

    if(fire){
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(vS[i][j]<vX[i][j]) out++;
            }
        }
    }
    else{
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(vS[i][j]>=0) out++;
            }
        }
    }


    cout<<out;

    return 0;
}