#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
  long long ans = 0;
  char maze[22][22];
  for (long long i = (0); i < (22); i++) {
    for (long long j = (0); j < (22); j++) {
      maze[i][j] = '#';
    }
  }
  long long h, w;
  cin >> h >> w;
  char s[w];
  for (long long i = (0); i < (h); i++) {
    cin >> s;
    for (long long j = (0); j < (w); j++) {
      maze[i + 1][j + 1] = s[j];
    }
  }
  long long stcnt = 0;
  for (long long i = (0); i < (22); i++) {
    for (long long j = (0); j < (22); j++) {
      if (maze[i][j] == '.') {
        if (maze[i + 1][j] == '#') {
          stcnt++;
        }
        if (maze[i - 1][j] == '#') {
          stcnt++;
        }
        if (maze[i][j + 1] == '#') {
          stcnt++;
        }
        if (maze[i][j - 1] == '#') {
          stcnt++;
        }
        if (stcnt == 3) {
          maze[i][j] = 'S';
        }
        stcnt = 0;
      }
    }
  }
  stcnt = 0;
  for (long long i = (0); i < (22); i++) {
    for (long long j = (0); j < (22); j++) {
      if (maze[i][j] == 'S') {
        stcnt++;
      }
    }
  }
  if (stcnt < 2) {
    cout << h + w - 2 << endl;
    return 0;
  }
  long long sh = 0;
  long long sw = 0;
  for (long long i = (0); i < (22); i++) {
    for (long long j = (0); j < (22); j++) {
      if (maze[i][j] == 'S') {
        sh = i;
        sw = j;
      }
    }
  }
  while (1) {
    if (maze[sh + 1][sw] == 'S') {
      ans++;
      break;
    }
    if (maze[sh][sw + 1] == 'S') {
      ans++;
      break;
    }
    if (maze[sh - 1][sw] == 'S') {
      ans++;
      break;
    }
    if (maze[sh][sw - 1] == 'S') {
      ans++;
      break;
    }
    if (maze[sh + 1][sw] == '.') {
      ans++;
      maze[sh][sw] = '#';
      sh = sh + 1;
      continue;
    }
    if (maze[sh][sw + 1] == '.') {
      ans++;
      maze[sh][sw] = '#';
      sw = sw + 1;
      continue;
    }
    if (maze[sh - 1][sw] == '.') {
      ans++;
      maze[sh][sw] = '#';
      sh = sh - 1;
      continue;
    }
    if (maze[sh][sw - 1] == '.') {
      ans++;
      maze[sh][sw] = '#';
      sw = sw - 1;
      continue;
    }
  }
  cout << ans << endl;
  return 0;
}
