#include <bits/stdc++.h>
using namespace std;
int h, w, k, x, y, x1, y1;
char A[1010][1010], s[1010];
int dot[1010][1010], mindis;
void dfs(int a, int b, int l) {
  if (a < 1 || b < 1 || a > h || b > w) return;
  if (dot[a][b] != -1 || A[a][b] == '#') return;
  dot[a][b] = l;
  dfs(a - 1, b, l + 1);
  dfs(a + 1, b, l + 1);
  dfs(a, b - 1, l + 1);
  dfs(a, b + 1, l + 1);
}
int dis(int a, int b) { return min(a - 1, min(b - 1, min(h - a, w - b))); }
int chu(int a) {
  if (a % k == 0) return a / k;
  return a / k + 1;
}
int main() {
  scanf("%d %d %d", &h, &w, &k);
  for (int i = 1; i <= h; i++) {
    scanf("%s", s);
    for (int j = 1; j <= w; j++) {
      A[i][j] = s[j - 1];
      if (A[i][j] == 'S') {
        x = i;
        y = j;
        A[i][j] = '.';
      }
    }
  }
  memset(dot, -1, sizeof(dot));
  dfs(x, y, 0);
  mindis = 1 << 30;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (dot[i][j] == -1) continue;
      if (mindis > dis(i, j)) {
        mindis = dis(i, j);
        x1 = i;
        y1 = j;
      }
    }
  }
  int t1 = chu(dot[x1][y1]), t2 = chu(dis(x1, y1));
  if (t1 < t2)
    printf("%d\n", t1 + t2 + 1);
  else
    printf("%d\n", t1 + t2);
  return 0;
}
