#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > a;
struct state_t {
  int r, x, y;
  bool operator<(const state_t& rhs) const {
    if (r > rhs.r) return 1;
    return 0;
  }
};
priority_queue<state_t> que;
int w, h;
void push(int x, int y, int r) { que.push((state_t){r, x, y}); }
int pop(int& x, int& y, int& r) {
  if (que.empty()) return 0;
  state_t s = que.top();
  x = s.x;
  y = s.y;
  r = s.r;
  que.pop();
  return 1;
}
void move(int sx, int sy, int k) {
  int vx[] = {1, 0, -1, 0};
  int vy[] = {0, -1, 0, 1};
  int x, y, r, nx, ny, i;
  push(sx, sy, k);
  while (pop(x, y, r)) {
    if (a[y][x] != '.') continue;
    a[y][x] = 'C';
    if (r <= 0) continue;
    for (i = 0; i < 4; i++) {
      nx = x + vx[i];
      ny = y + vy[i];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
      push(nx, ny, r - 1);
    }
  }
}
int min_u(int& m, int v) {
  if (m > v) {
    m = v;
    return 1;
  }
  return 0;
}
int main(void) {
  int k;
  int x, y, sx, sy, min;
  while (scanf("%d%d%d", &h, &w, &k) == 3) {
    a.resize(h);
    for (auto& ax : a) ax.resize(w);
    for (y = 0; y < h; y++) {
      char buf[800 + 2];
      scanf("%s", buf);
      for (x = 0; x < w; x++) {
        a[y][x] = buf[x];
        if (buf[x] == 'S') {
          sx = x;
          sy = y;
          a[y][x] = '.';
        }
      }
    }
    move(sx, sy, k);
    min = (sx + k - 1) / k + 1;
    for (y = 0; y < h; y++) {
      for (x = 0; x < w; x++) {
        if (a[y][x] == 'C') {
          min_u(min, (x + k - 1) / k + 1);
          min_u(min, (w - 1 - x + k - 1) / k + 1);
          min_u(min, (y + k - 1) / k + 1);
          min_u(min, (h - 1 - y + k - 1) / k + 1);
        }
      }
    }
    printf("%d\n", min);
  }
  return 0;
}
