#include <bits/stdc++.h>
using namespace std;
const long long int E = 1e18 + 7;
const long long int MOD = 1000000007;
int main() {
  long long int h, w, k;
  cin >> h >> w >> k;
  vector<string> A(h);
  for (int i = 0; i < h; i++) {
    cin >> A[i];
  }
  pair<long long int, long long int> s;
  for (int i = 0; i < h; i++) {
    for (int t = 0; t < w; t++) {
      if (A[i][t] == 'S') {
        s = {i, t};
      }
    }
  }
  long long int ans = max(
      min(min(s.first, s.second), min(h - 1 - s.first, w - 1 - s.second)), k);
  queue<pair<long long int, long long int>> Q;
  Q.push(s);
  vector<vector<long long int>> dist(h, vector<long long int>(w, E));
  dist[s.first][s.second] = 0;
  vector<pair<long long int, long long int>> d = {
      {0, 1}, {1, 0}, {-1, 0}, {0, -1}};
  bool done = false;
  while (!Q.empty() && !done) {
    pair<long long int, long long int> W = Q.front();
    Q.pop();
    for (int i = 0; i < 4 && !done; i++) {
      pair<long long int, long long int> k = W;
      k.first += d[i].first;
      k.second += d[i].second;
      if (A[k.first][k.second] == '#' || dist[k.first][k.second] != E) {
        continue;
      }
      dist[k.first][k.second] = dist[W.first][W.second] + 1;
      Q.push({k.first, k.second});
      if (k.first == 0 || k.second == 0 || k.first == h - 1 ||
          k.second == w - 1) {
        ans = min(ans, dist[k.first][k.second]);
        done = true;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
