#include <bits/stdc++.h>
using namespace std;
char maps[805][805];
int dis[805][805];
queue<int> xv, yv;
int main() {
  int x, y, h, w, k, ans = 666666;
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) {
    scanf("%s", maps[i] + 1);
    for (int j = 1; j <= w; j++) {
      dis[i][j] = -1;
      if (maps[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
  }
  xv.push(x);
  yv.push(y);
  dis[x][y] = 0;
  while (!xv.empty()) {
    x = xv.front();
    xv.pop();
    y = yv.front();
    yv.pop();
    int temp = min(min(x - 1, h - x), min(y = 1, w - y));
    temp = temp / k + (temp % k != 0);
    ans = min(ans, 1 + temp);
    if (dis[x][y] < k) {
      if (dis[x + 1][y] == -1 && maps[x + 1][y] == '.') {
        xv.push(x + 1);
        yv.push(y);
        dis[x + 1][y] = dis[x][y] + 1;
      }
      if (dis[x - 1][y] == -1 && maps[x - 1][y] == '.') {
        xv.push(x - 1);
        yv.push(y);
        dis[x - 1][y] = dis[x][y] + 1;
      }
      if (dis[x][y + 1] == -1 && maps[x][y + 1] == '.') {
        xv.push(x);
        yv.push(y + 1);
        dis[x][y + 1] = dis[x][y] + 1;
      }
      if (dis[x][y - 1] == -1 && maps[x][y - 1] == '.') {
        xv.push(x);
        yv.push(y - 1);
        dis[x][y - 1] = dis[x][y] + 1;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
