#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char a[801][801];
int d[801][801];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int intceil(int a, int b) { return a / b + (a % b > 0); }
void bfs(int i, int j) {
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  memset(d, -1, sizeof(d));
  pq.push({0, {i, j}});
  d[i][j] = 0;
  while (!pq.empty()) {
    auto u = pq.top();
    int cost = u.first;
    i = u.second.first;
    j = u.second.second;
    pq.pop();
    if (cost > k) break;
    for (int di = 0; di < 4; ++di) {
      int ni = i + dy[di];
      int nj = j + dx[di];
      if (a[ni][nj] == '#') continue;
      int nc = cost + 1;
      if (d[ni][nj] == -1 || nc < d[ni][nj]) {
        d[ni][nj] = nc;
        pq.push({nc, {ni, nj}});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int I, J, ans = 801;
  cin >> h >> w >> k;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        I = i;
        J = j;
      }
    }
  }
  if (I == 0 || I == h - 1 || J == 0 || J == w - 1) {
    cout << 0 << endl;
    return 0;
  }
  bfs(I, J);
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (d[i][j] > -1)
        ans =
            min(ans, intceil(min(min(i, h - 1 - i), min(j, w - 1 - j)), k) + 1);
    }
  }
  cout << ans << endl;
  return 0;
}
