#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C>
void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  for (int i = 0; i < h; ++i) {
    cin >> a[i];
  }
  int sx, sy;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (a[i][j] == 'S') {
        sy = i;
        sx = j;
        break;
      }
    }
  }
  vvi first(h, vi(w, 0));
  first[sy][sx] = 1;
  deque<pii> que;
  que.emplace_front(sy, sx);
  const int dx[4] = {-1, 1, 0, 0};
  const int dy[4] = {0, 0, -1, 1};
  while (que.size() != 0) {
    auto p = que.back();
    que.pop_back();
    for (int i = 0; i < 4; ++i) {
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w && a[ny][nx] != '#') {
        que.emplace_front(ny, nx);
        a[ny][nx] = '#';
        first[ny][nx] = 1;
      }
    }
  }
  int ret = 100000;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (first[i][j] == 1) {
        chmin(ret, min(i, h - i - 1));
        chmin(ret, min(j, w - j - i));
      }
    }
  }
  cout << ret + 1 << "\n";
  return 0;
}
