#include <bits/stdc++.h>
using namespace std;
struct pos {
  int x, y;
};
const vector<pos> moves{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int h, w;
bool inRange(pos p) { return 0 <= p.x && p.x < h && 0 <= p.y && p.y < w; }
int calc(pos p) { return min({p.x, h - p.x - 1, p.y, w - p.y - 1}); }
int main() {
  int k;
  cin >> h >> w >> k;
  pos start;
  vector<vector<bool>> isWall(h, vector<bool>(w));
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == '#') isWall[i][j] = true;
      if (s[j] == 'S') start = {i, j};
    }
  }
  queue<pos> que;
  que.push(start), que.push({-1, -1});
  int dis = 1;
  int ret = 1145141919;
  while (dis <= k) {
    pos now = que.front();
    que.pop();
    if (now.x < 0) {
      dis++;
      que.push({-1, -1});
    } else {
      int cost = (calc(now) + k - 1) / k;
      ret = min(ret, 1 + cost);
      for (auto move : moves) {
        pos next = {now.x + move.x, now.y + move.y};
        if (inRange(next) && !isWall[next.x][next.y]) {
          isWall[next.x][next.y] = true;
          que.push(next);
        }
      }
    }
  }
  assert(ret != 1141541919);
  cout << ret << endl;
  return 0;
}
