#include <bits/stdc++.h>
using namespace std;
struct o {
  int y, x, d;
};
int h, w, k, sy, sx, ans = 1000, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
bool m[805][805];
int r(int y, int x) {
  return (min(min(x, w - x - 1), min(y, h - y - 1)) + k - 1) / k;
}
int main(void) {
  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 == '#')
        m[i][j] = true;
      else if (c == 'S') {
        sy = i;
        sx = j;
      }
    }
  }
  queue<o> bfs;
  bfs.push(o{sy, sx, 0});
  while (!bfs.empty()) {
    o u = bfs.front();
    bfs.pop();
    m[u.y][u.x] = true;
    ans = min(ans, r(u.y, u.x));
    for (int i = 0; i < 4; i++) {
      int x = u.x + dx[i], y = u.y + dy[i];
      if (0 < x && x < w && y > 0 && y < h && !m[y][x] && u.d < k)
        bfs.push(o{y, x, u.d + 1});
    }
  }
  printf("%d\n", ans + 1);
}
