#include <iostream>
#include <utility>
#include <queue>

void matrica_citaj(char** &matrica, int m, int n){
  matrica = new char*[m];
  for (int i = 0; i < m; ++i){
    matrica[i] = new char[n]();
  }
  for (int i = 0; i < m; i++){
    for (int j = 0; j < n; j++){
      std::cin >> matrica[i][j];
    }
  }
}

void matrica_pisi(char** matrica, int m, int n){
  for(int i = 0; i < m; i++){
    for(int j = 0; j < n; j++){
      std::cout << matrica[i][j] << " ";
    }
    std::cout << "\n";
  }
}

int main(){
  char **lavirint;
  int m, n;
  std::cin >> m >> n;
  matrica_citaj(lavirint, m, n);

  std::queue<std::pair<int, int>> sq;
  std::queue<std::pair<int, int>> vq;

  for(int i = 0; i < m; i++){
    for(int j = 0; j < n; j++){
      if(lavirint[i][j] == 'S')
        sq.push(std::make_pair(i, j));
      else if(lavirint[i][j] == 'X')
        vq.push(std::make_pair(i, j));
    }
  }
  while(!sq.empty()){
    int vs = vq.size();
    int ss = sq.size();
    while(vs-- != 0 && !vq.empty()){
      std::pair<int, int> v = vq.front();
      vq.pop();
      if(v.first < m-1 && lavirint[v.first+1][v.second] == '.'){
        vq.push(std::make_pair(v.first+1, v.second));
        lavirint[v.first+1][v.second] = 'X';
      }
      if(v.first > 0 && lavirint[v.first-1][v.second] == '.'){
        vq.push(std::make_pair(v.first-1, v.second));
        lavirint[v.first-1][v.second] = 'X';
      }
      if(v.second < n-1 && lavirint[v.first][v.second+1] == '.'){
        vq.push(std::make_pair(v.first, v.second+1));
        lavirint[v.first][v.second+1] = 'X';
      }
      if(v.second > 0 && lavirint[v.first][v.second-1] == '.'){
        vq.push(std::make_pair(v.first, v.second-1));
        lavirint[v.first][v.second-1] = 'X';
      }
    }

    while(ss-- != 0){
      std::pair<int, int> s = sq.front();
      sq.pop();
      if(s.first < m-1 && lavirint[s.first+1][s.second] == '.'){
        sq.push(std::make_pair(s.first+1, s.second));
        lavirint[s.first+1][s.second] = 'S';
      }
      if(s.first > 0 && lavirint[s.first-1][s.second] == '.'){
        sq.push(std::make_pair(s.first-1, s.second));
        lavirint[s.first-1][s.second] = 'S';
      }
      if(s.second < n-1 && lavirint[s.first][s.second+1] == '.'){
        sq.push(std::make_pair(s.first, s.second+1));
        lavirint[s.first][s.second+1] = 'S';
      }
      if(s.second > 0 && lavirint[s.first][s.second-1] == '.'){
        sq.push(std::make_pair(s.first, s.second-1));
        lavirint[s.first][s.second-1] = 'S';
      }
    }
  }
  int sc = 0;
  for(int i = 0; i < m; i++){
    for(int j = 0; j < n; j++){
      sc += lavirint[i][j] == 'S';
    }
  }
  std::cout << sc << std::endl;
}