#include<bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m, B = 0;
  bool a = true;
  cin >> n >> m;
  vector<string> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  vector<pair<int, int>> vs, vx, vsl, vxl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      pair<int, int> p = {i, j};
      if (v[i][j] == 'S')
        vs.push_back(p);
      else if (v[i][j] == 'X')
        vx.push_back(p);
    }
  }
  while (a) {
    a = false;
    vsl = vs;
    vxl = vx;
    vs.clear();
    vx.clear();
    for (int h = 0; h < vxl.size(); h++) {
      int i = vxl[h].first, j = vxl[h].second;
      if (i > 0 and v[i - 1][j] == '.') {
        v[i - 1][j] = 'X';
        vx.push_back({i - 1, j});
      }
      if (i < n - 1 and v[i + 1][j] == '.') {
        v[i + 1][j] = 'X';
        vx.push_back({i + 1, j});
      }
      if (j > 0 and v[i][j - 1] == '.') {
        v[i][j - 1] = 'X';
        vx.push_back({i, j - 1});
      }
      if (j < m - 1 and v[i][j + 1] == '.') {
        v[i][j + 1] = 'X';
        vx.push_back({i, j + 1});
      }
    }
    for (int h = 0; h < vsl.size(); h++) {
      int i = vsl[h].first, j = vsl[h].second;
      if (i > 0 and v[i - 1][j] == '.') {
        v[i - 1][j] = 'S';
        vs.push_back({i - 1, j});
      }
      if (i < n - 1 and v[i + 1][j] == '.') {
        v[i + 1][j] = 'S';
        vs.push_back({i + 1, j});
      }
      if (j > 0 and v[i][j - 1] == '.') {
        v[i][j - 1] = 'S';
        vs.push_back({i, j - 1});
      }
      if (j < m - 1 and v[i][j + 1] == '.') {
        v[i][j + 1] = 'S';
        vs.push_back({i, j + 1});
      }
    }
    if (vs.size() > 0)
      a = true;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (v[i][j] == 'S')
        B++;
    }
  }
  cout << B;
  return 0;
}