#include <bits/stdc++.h>

using namespace std;

vector<string> lav;
vector<vector<int>> bv;
vector<vector<int>> bs;
short int n,m;
int s=1;
const int smerX[]={-1,1,0,0};
const int smerY[]={0,0,-1,1};

void siri(int x,int y,char vr,int t){
    /*//if(vr=='V')
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            cout<<bv[i][j]<<"x";
        cout<<endl;
    }//else
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            cout<<bs[i][j]<<"v";
        cout<<endl;
    }*/
    if(bs[x][y]&&vr=='S'){
        if(t<bs[x][y])
            bs[x][y]=t;
        else
            return;
    }
    if(bv[x][y]&&vr=='X'){
        if(t<bv[x][y])
            bv[x][y]=t;
        else
            return;
    }
    if(vr=='S')
        bs[x][y]+=t;
    else if(vr=='X')
        bv[x][y]=t;
    for(int i=0;i<4;i++){
        //cout<<x+smerX[i]<<" "<<y+smerY[i]<<endl;
        if(x+smerX[i]>=0&&x+smerX[i]<n&&y+smerY[i]>=0&&y+smerY[i]<m&&lav[x+smerX[i]][y+smerY[i]]=='.')
            siri(x+smerX[i],y+smerY[i],vr,t+1);
    }
}

int main()
{
    short int i,j;
    string vr;
    cin>>n>>m;
    bv.assign(n,vector<int>(m));
    bs.assign(n,vector<int>(m));
    for(i=0;i<n;i++){
        cin>>vr;
        lav.push_back(vr);
    }
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(lav[i][j]=='S'){
                siri(i,j,'S',0);
                break;
            }
        }
    }
    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            if(lav[i][j]=='X')
                siri(i,j,'X',0);
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(bs[i][j]<bv[i][j])s++;
        }
    }
    cout<<s;
    return 0;
}