#include <bits/stdc++.h>

using namespace std;

vector<string> lav;
vector<vector<int>> bv;
vector<vector<int>> bs;
short int n,m;
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(t>20)return;
    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;
    }
    /*cout<<t<<endl;
    if(vr=='X')
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            cout<<bv[i][j]<<" v ";
        cout<<endl;
    }else
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            cout<<bs[i][j]<<" s ";
        cout<<endl;
    }*/
    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]]!='*'&&lav[x+smerX[i]][y+smerY[i]]!=vr)
            siri(x+smerX[i],y+smerY[i],vr,t+1);
    }
}

int main()
{
    short int i,j;
    bool vtr=0;
    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]=='X'){
                vtr=1;
                siri(i,j,'X',0);
            }
    int s=1;
    if(!vtr){
        for(i=0;i<n;i++)
            s+=count(lav[i].begin(),lav[i].end(),'.');
    }else{
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                if(lav[i][j]=='S'){
                    siri(i,j,'S',0);
                    break;
                }
            }
        }
        /*for(int i=0;i<n;i++){
            for(int j=0;j<m;j++)
                cout<<bv[i][j]<<" v ";
            cout<<endl;
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++)
                cout<<bs[i][j]<<" s ";
            cout<<endl;
        }*/
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                if((bs[i][j]<bv[i][j]&&bs[i][j])||(bv[i][j]==0&&bs[i][j]&&lav[i][j]!='X')){
                    //cout<<bs[i][j]<<" "<<bv[i][j]<<endl;
                    s++;
                }
            }
        }
    }
    cout<<s;
    return 0;
}