#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<int> > mp(h, vector<int>(w, 0));
  vector<vector<int> > d3(h, vector<int>(w, -1));
  char c;
  pair<int, int> p;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c;
      if (c == '#') {
        mp[i][j] = 1;
      } else if (c == 'S') {
        mp[i][j] = 0;
        p.first = i;
        p.second = j;
      } else {
        mp[i][j] = 0;
      }
    }
  }
  queue<pair<int, int> > qq;
  qq.push(p);
  d3[p.first][p.second] = 0;
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  vector<pair<int, int> > d4;
  d4.push_back(p);
  for (int i = 0; i < k; i++) {
    queue<pair<int, int> > q3;
    while (!qq.empty()) {
      pair<int, int> rr = qq.front();
      int y = rr.first;
      int x = rr.second;
      qq.pop();
      for (int j = 0; j < 4; j++) {
        if (0 <= y + dy[j] && y + dy[j] < h && 0 <= x + dx[j] &&
            x + dx[j] < w && mp[y + dy[j]][x + dx[j]] == 0 &&
            d3[y + dy[j]][x + dx[j]] == -1) {
          d3[y + dy[j]][x + dx[j]] = 0;
          q3.push(pair<int, int>(y + dy[j], x + dx[j]));
          d4.push_back(pair<int, int>(y + dy[j], x + dx[j]));
        }
      }
    }
    qq = q3;
  }
  int ans = 10000;
  for (int i = 0; i < d4.size(); i++) {
    int dist = min(d4[i].first, min(h - d4[i].first - 1,
                                    min(d4[i].second, w - d4[i].second)));
    if (dist == 0) {
      ans = 1;
    } else {
      int aa = dist / k;
      if (dist % k != 0) {
        aa++;
      }
      aa++;
      ans = min(ans, aa);
    }
  }
  cout << ans << endl;
  return 0;
}
