#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000007;
int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, 1, -1, 0};
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<bool> > G(h + 2, vector<bool>(w + 2, false));
  int si, sj;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      char in;
      cin >> in;
      if (in == '#') continue;
      G[i][j] = true;
      if (in == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  vector<pair<int, int> > ans;
  G[si][sj] = false;
  queue<pair<int, pair<int, int> > > que;
  que.push({k, {si, sj}});
  while (!que.empty()) {
    pair<int, pair<int, int> > info = que.front();
    que.pop();
    int i = info.second.first;
    int j = info.second.second;
    ans.push_back({i, j});
    for (int h = 0; h < 4 && info.first > 0; h++) {
      int ni = i + di[h];
      int nj = j + dj[h];
      if (G[ni][nj]) {
        G[ni][nj] = false;
        que.push({info.first - 1, {ni, nj}});
      }
    }
  }
  int anss = INF;
  for (int i = 0; i < ans.size(); i++) {
    int tmp = min(min(ans[i].first - 1, ans[i].second - 1),
                  min(h - ans[i].first, w - ans[i].second));
    if (tmp != 0) anss = min(anss, 2 + (tmp - 1) / k);
    if (tmp == 0) anss = 1;
  }
  cout << anss << endl;
  return 0;
}
