#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  int h, w, k;
  char map[800][800];
  pair<int, int> start;
  while (cin >> h >> w >> k) {
    queue<pair<int, int>> que;
    queue<pair<int, int>> que2;
    bool isClear = false;
    int ans = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        char c;
        cin >> c;
        map[i][j] = c;
        if (c == 'S') {
          start = make_pair(j, i);
        }
      }
    }
    que.push(start);
    while (!que.empty() && !isClear) {
      pair<int, int> p = que.front();
      que.pop();
      int x = p.first;
      int y = p.second;
      for (int i = 0; i < 4; ++i) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || w <= nx || ny < 0 || h <= ny) {
          isClear = true;
          break;
        }
        char n = map[ny][nx];
        pair<int, int> np = make_pair(nx, ny);
        if (n == '.') {
          que.push(np);
        } else if (n == '#') {
          que2.push(np);
        }
      }
      if (que.empty() && !que2.empty()) {
        que.push(que2.front());
        que2.pop();
        ++ans;
      }
    }
    cout << ans << endl;
  }
}
