#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> rooms(H);
  int sy, sx;
  for (int i = 0; i < (int)(H); ++i) {
    cin >> rooms[i];
    auto idx = rooms[i].find("S");
    if (idx != string::npos) {
      sy = i;
      sx = idx;
    }
  }
  queue<pair<int, pair<int, int>>> Q;
  Q.push({0, {sy, sx}});
  set<pair<int, int>> S;
  S.insert({sy, sx});
  while (!Q.empty()) {
    pair<int, pair<int, int>> q;
    q = Q.front();
    Q.pop();
    if (q.first == K) break;
    for (int i = 0; i < (int)(4); ++i) {
      int ny = q.second.first + dy[i];
      int nx = q.second.second + dx[i];
      if ((0 <= ny && ny < H && 0 <= nx && nx < W)) {
        if (rooms[ny][nx] != '#' && S.find(q.second) == S.end()) {
          S.insert(q.second);
          Q.push({q.first + 1, {ny, nx}});
        }
      }
    }
  }
  int ans = 1e9;
  for (auto x : S) {
    ans = min(ans, (min(min(abs(x.first - 0), abs(x.first - H + 1)),
                        min(abs(x.second - 0), abs(x.second - W + 1))) +
                    (K - 1)) /
                           K +
                       1);
  }
  cout << ans << endl;
  return 0;
}
