#include <bits/stdc++.h>
using namespace std;
void solve();
int H, W, K;
int fid[1000][1000] = {0};
int sx, sy;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int minx = 1000, maxx = -1, miny = 1000, maxy = -1;
int main() {
  scanf("%d%d%d", &H, &W, &K);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      scanf(" %c", &c);
      if (c == '#') {
        fid[i][j] = -1;
      }
      if (c == 'S') {
        sx = j;
        sy = i;
      }
    }
  }
  solve();
  return 0;
}
void solve() {
  queue<pair<int, pair<int, int> > > Q;
  Q.push(make_pair(K, make_pair(sy, sx)));
  while (Q.size()) {
    pair<int, pair<int, int> > now = Q.front();
    Q.pop();
    if (fid[now.second.first][now.second.second]) continue;
    fid[now.second.first][now.second.second] = now.first;
    if (now.second.first < minx) minx = now.second.first;
    if (now.second.first > maxx) maxx = now.second.first;
    if (now.second.second < miny) miny = now.second.second;
    if (now.second.second > maxy) maxy = now.second.second;
    if (now.first == 0) continue;
    for (int i = 0; i < 4; i++) {
      int x = now.second.first + dx[i], y = now.second.second + dy[i];
      if (0 <= x && x < W && 0 <= y && y < H) {
        Q.push(make_pair(now.first - 1, make_pair(x, y)));
      }
    }
  }
  int ans = min(min(minx, W - maxx - 1), min(miny, H - maxy - 1));
  printf("%d", (ans + K - 1) / K + 1);
  return;
}
