#include<iostream>
#include<queue>
using namespace std;
typedef unsigned long long ullong;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    unsigned x,y,a,b,n;
    cin>>x>>y;
    x+=2;y+=2;
    queue<unsigned>e;
    char**board=new char*[x];
    for(unsigned i=0;i<x;++i){
        board[i]=new char[y];
        board[i][0]='*';
        board[i][y-1]='*';}
    x-=2;y-=2;
    for(unsigned i=1;i<=y;++i){
        board[0][i]='*';
        board[x+1][i]='*';}
    string s;char c;
    for(a=1;a<=x;++a){
        cin>>s;
        for(b=1;b<=y;++b){
            board[a][b]=c=s[b-1];
            if(c=='X')
                e.push(a|(b<<16));
            else if(c=='S')n=(a|(b<<16));}}
    e.push(n);n=1;
    bool q;
    while(!e.empty()){
        a=e.front();
        b=(a>>16);
        a&=65535u;
        e.pop();
        c=board[a][b];
        q=c=='S';
        if(board[a+1][b]=='.'){board[a+1][b]=c;e.push((a+1)|(b<<16));n+=q;}
        if(board[a][b+1]=='.'){board[a][b+1]=c;e.push(a|((b+1)<<16));n+=q;}
        if(board[a-1][b]=='.'){board[a-1][b]=c;e.push((a-1)|(b<<16));n+=q;}
        if(board[a][b-1]=='.'){board[a][b-1]=c;e.push(a|((b-1)<<16));n+=q;}}
    cout<<n;
    return 0;}