#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define endl "\n"
char mat[1015][1015];
int brojevi[1015][1015],ans=0,n,m;
void start(int x,int y,int broj){
    if(x<0 || x>n || y<0 || y>m){/**cout<<"1\n";*/return;}
    if(mat[x][y]!='.'){/**cout<<"2\n";*/return;}
    if(brojevi[x][y]>0){/**cout<<"3\n";*/return;}
    brojevi[x][y]=broj;
    broj++;ans++;
    int dx[]={1,0,-1,0};
    int dy[]={0,1,0,-1};
    for(int i=0;i<4;i++){
        start(x+dx[i],y+dy[i],broj);
    }
}
void start(int x,int y){
    ans++;
    int dx[]={1,0,-1,0};
    int dy[]={0,1,0,-1};
    for(int i=0;i<4;i++){
        start(x+dx[i],y+dy[i],1);
    }
}
void end(int x,int y,int broj){
    if(x<0 || x>n || y<0 || y>m){/**cout<<"1\n";*/return;}
    if(mat[x][y]!='.'){/**cout<<"2\n";*/return;}
    if(brojevi[x][y]<=0){/**cout<<"3\n";*/return;}
    if(broj*(-1)>brojevi[x][y] && brojevi[x][y]>0)return;
    brojevi[x][y]=broj;
    broj--;ans--;
    int dx[]={1,0,-1,0};
    int dy[]={0,1,0,-1};
    for(int i=0;i<4;i++){
        end(x+dx[i],y+dy[i],broj);
    }
}
void solve(){
    int sx,sy;cin>>n>>m;
    vector<array<int,2>>x;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            char c;cin>>c;
            mat[i][j]=c;
            brojevi[i][j]=0;
            if(c=='S'){sx=i;sy=j;}
            if(c=='X'){brojevi[i][j]=-1;x.push_back({i,j});}
            if(c=='*'){brojevi[i][j]=-1;}
        }
    }
    start(sx,sy);

    for(int i=0;i<x.size();i++){
        auto [xi,yi]=x[i];
        ///cout<<xi<<" "<<yi<<endl;
        int dx[]={1,0,-1,0};
        int dy[]={0,1,0,-1};
        for(int i=0;i<4;i++){
            end(xi+dx[i],yi+dy[i],-1);
        }
    }
    cout<<ans<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    int t=1;//cin>>t;
    while(t--)solve();
    return 0;
}