#include<bits/stdc++.h>
using namespace std;
int N, M, dr[4] = {0, 1, 0, -1}, dc[4] = {1, 0, -1, 0}, br = 1;
char m[1005][1005];
queue<int> Qr, Qc;
void bfs()
{
  while(Qr.size()){
    int r = Qr.front();
    int c = Qc.front();
    char cc = m[r][c];
    for(int i = 0; i < 4; i++){
      if(cc == 'S'){
        if(m[r + dr[i]][c + dc[i]] == '.'){
          Qr.push(r + dr[i]);
          Qc.push(c + dc[i]);
          m[r + dr[i]][c + dc[i]] = 'S';
          br++;
        }
      }
      if(cc == 'X'){
        if(m[r + dr[i]][c + dc[i]] == '.'){
          Qr.push(r + dr[i]);
          Qc.push(c + dc[i]);
          m[r + dr[i]][c + dc[i]] = 'X';
        }
      }
    }
    Qr.pop();
    Qc.pop();

  }

}
int main(){
  int Sr, Sc;
  scanf("%d %d", &N, &M);
  memset(m, '*', sizeof(m));
  for(int i = 1; i <= N; i++){
    for(int j = 1; j <= M; j++){
      cin >> m[i][j];
      if(m[i][j] == 'X')Qr.push(i), Qc.push(j);
      if(m[i][j] == 'S')Sr = i, Sc = j;
    }
  }
  Qr.push(Sr);
  Qc.push(Sc);
  bfs();
//  for(int i = 1; i <= N; i++){
//    for(int j = 1; j <= M; j++)cout << m[i][j] << ' ';
//    cout << endl;
//  }
  cout << br << endl;

return 0;
}