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

static int maxe=1e5;
struct XY{
    int x;
    int y;
};

int putanjaVatre(vector<XY>,char[],int[4][2],int[],int,int);
int putanjaSteva(vector<XY>,char[],int[4][2],int[],int[],int,int);
int inbound(int,int);
            


int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //UNOS
    int N,M;
    cin>>N>>M;
    char suma[N][M];
    int putVatre[N*M];
    int putSteve[N*M];
    
    vector<XY>steve,vatrica;
    int putanja[4][2]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    //DOBIJANJE MATRICE I POCETKA VATRE/STEVA
    for(int i=0;i<N;++i){
        for(int j=0;j<M;++j){
            cin>>suma[i][j];
            if(suma[i][j]=='S'){
                putSteve[i*M+j]=0;
                putVatre[i*M+j]=maxe;
                steve.push_back({i,j});
            }
            if(suma[i][j]=='X'){
                putSteve[i*M+j]=maxe;
                putVatre[i*M+j]=0;
                vatrica.push_back({i,j});
            }
            if(suma[i][j]=='.'){
                putSteve[i*M+j]=maxe;
                putVatre[i*M+j]=maxe;
            }
            if(suma[i][j]=='*'){
                putSteve[i*M+j]=-1;
                putVatre[i*M+j]=-1;
            }
        
        }
    }
    //TRANSFORMACIJA SUME U NIZ
    char sumetina[N*M];
    for (int i = 0; i < N; ++i) 
        for (int j = 0; j < M; ++j) 
            sumetina[i * M + j] = suma[i][j];
    
    putanjaVatre(vatrica,sumetina,putanja,putVatre,N,M);
    putanjaSteva(steve,sumetina,putanja,putSteve,putVatre,N,M);
            
    return 0;
    }

int putanjaVatre(vector<XY> vatrica, char sumetina[], int putanja[4][2], int putVatre[], int N, int M) {
  
    int x, nx, y, ny;
    
    for (int i = 0; i < vatrica.size(); ++i) {
        x = vatrica[i].x;
        y = vatrica[i].y;
        
        for (int put = 0; put < 4; ++put) {
            nx = x + putanja[put][0];
            ny = y + putanja[put][1];
            
            if (inbound(nx, N) && inbound(ny, M)) {
                if ((sumetina[nx * M + ny] == '.' || sumetina[nx * M + ny] == 'S' ) && putVatre[x * M + y] + 1<putVatre[nx * M + ny]) {
                    vatrica.push_back({nx, ny});
                    putVatre[nx * M + ny] = putVatre[x * M + y] + 1;
                    }
                }
            }
        }

    return 0;
}  
int putanjaSteva(vector<XY> steve, char sumetina[], int putanja[4][2], int putSteve[],int putVatre[], int N, int M) {
    int x, nx, y, ny;
    
    for (int i = 0; i < steve.size(); ++i) {
        x = steve[i].x;
        y = steve[i].y;
        
        for (int put = 0; put < 4; ++put) {
            nx = x + putanja[put][0];
            ny = y + putanja[put][1];
            
            if (inbound(nx, N) && inbound(ny, M)) {
                if ((sumetina[nx * M + ny] == '.' || sumetina[nx * M + ny] == 'X' ) && putSteve[x * M + y] + 1<putSteve[nx * M + ny]) {
                    steve.push_back({nx, ny});
                    putSteve[nx * M + ny] = putSteve[x * M + y] + 1;
                    }
                }
            }
        }
    int pomocna=0;
    for(int i=0;i<N;++i)
      for(int j=0;j<M;++j){
        if(putSteve[i*M+j]<putVatre[i*M+j] && putSteve[i*M+j]!=-1 && putVatre[i*M+j] ){
          pomocna++;
        }
      }
    cout<<pomocna;
    return 0;
}  

int inbound(int xy,int NM){
  if(xy<NM && xy>=0)
    return true;
  return false;
}