#include <bits/stdc++.h>
using namespace std;
int pos(int y, int x) { return 1000 * y + x; }
bool visited[100000];
int main() {
  for (int i = (0); i < (100000); ++i) visited[i] = false;
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> vs;
  int S;
  for (int i = (0); i < (H); ++i) {
    string in;
    cin >> in;
    vs.push_back(in);
    for (int j = (0); j < (in.length()); ++j) {
      if (in[j] == 'S') {
        S = pos(i, j);
      }
    }
  }
  int vx[4] = {0, 1, 0, -1};
  int vy[4] = {-1, 0, 1, 0};
  queue<int> que;
  que.push(S);
  visited[S] = true;
  int MIN = min(min(S / 1000, abs(H - 1 - S / 1000)),
                min(S % 1000, abs(W - 1 - S % 1000)));
  int k = 0;
  while (!que.empty()) {
    int q = que.front();
    que.pop();
    int nowy = q / 1000;
    int nowx = q % 1000;
    for (int i = (0); i < (4); ++i) {
      int nexty = nowy + vy[i];
      int nextx = nowx + vx[i];
      if (vs[nexty][nextx] == '#' || visited[pos(nexty, nextx)]) continue;
      que.push(pos(nexty, nextx));
      visited[pos(nexty, nextx)] = true;
      MIN = min(MIN, min(min(nexty, abs(H - 1 - nexty)),
                         min(nextx, abs(W - 1 - nextx))));
    }
    k++;
    if (k == K) break;
  }
  if (MIN == 0)
    cout << 1 << endl;
  else
    cout << (MIN + K - 1) / K + 1 << endl;
}
