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

char matrica[1005][1005];
int N,M;

queue<int> qStevaR,qStevaC,qVatraR,qVatraC;
queue<int> QStevaR,QStevaC,QVatraR,QVatraC;
bool ch=true;
int sol=1;

int main(){
    ios_base::sync_with_stdio(false);
    cin>>N>>M;
    for(int i=1;i<N;++i)
    for(int j=1;j<M;++j){
        cin>>matrica[i][j];
        if(matrica[i][j]=='S'){
            qStevaR.push(i);
            qStevaC.push(j);
        }
        if(matrica[i][j]=='X'){
            qVatraR.push(i);
            qVatraC.push(j);
        }
    }
    while(ch){
        ch=false;
        while(!qVatraR.empty()){
            int i=qVatraC.front();
            qVatraC.pop();
            int j=qVatraR.front();
            qVatraR.pop();
            if(matrica[i-1][j]=='.'){
                matrica[i-1][j]='X';
                QVatraC.push(i-1);
                QVatraR.push(j);
                ch=true;
            }
            if(matrica[i][j-1]=='.'){
                matrica[i][j-1]='X';
                QVatraC.push(i);
                QVatraR.push(j-1);
                ch=true;
            }
            if(matrica[i+1][j]=='.'){
                matrica[i+1][j-1]='X';
                QVatraC.push(i+1);
                QVatraR.push(j-1);
                ch=true;
            }
            if(matrica[i][j+1]=='.'){
                matrica[i][j+1]='X';
                QVatraC.push(i);
                QVatraR.push(j+1);
                ch=true;
            }
        }
        while(!qStevaR.empty()){
            int i=qStevaC.front();
            qStevaC.pop();
            int j=qStevaR.front();
            qStevaR.pop();
            if(matrica[i-1][j]=='.'){
                matrica[i-1][j]='S';
                QStevaC.push(i-1);
                QStevaR.push(j);
                ch=true;
                ++sol;
            }
            if(matrica[i][j-1]=='.'){
                matrica[i][j-1]='S';
                QStevaC.push(i);
                QStevaR.push(j-1);
                ch=true;
                ++sol;
            }
            if(matrica[i+1][j]=='.'){
                matrica[i+1][j-1]='S';
                QStevaC.push(i+1);
                QStevaR.push(j-1);
                ch=true;
            }
            if(matrica[i][j+1]=='.'){
                matrica[i][j+1]='S';
                QStevaC.push(i);
                QStevaR.push(j+1);
                ch=true;
            }
        }
        swap(qStevaC,QStevaC);
        swap(qStevaR,QStevaR);
        swap(qVatraC,QVatraC);
        swap(qVatraR,QVatraR);
    }
    cout<<sol<<'\n';
    return 0;
}