#include <bits/stdc++.h>
int H, W, K;
char A[805][805];
void dfs(int posx, int posy, int depth);
int min(int a, int b);
int main() {
  scanf("%d %d %d", &H, &W, &K);
  int i, j;
  int startx, starty;
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      scanf(" %c", &A[i][j]);
      if (A[i][j] == 'S') {
        startx = j;
        starty = i;
      }
    }
  }
  if (startx == W - 1 || starty == H - 1) {
    printf("0\n");
  } else {
    dfs(startx, starty, K);
    int ans = 114514;
    for (i = 0; i < H; i++) {
      for (j = 0; j < W; j++) {
        if (A[i][j] == 'S') {
          int tmp = min(min(min(H - 1 - i, i), W - 1 - j), j);
          if (tmp % K == 0) {
            tmp = tmp / K;
            tmp++;
          } else {
            tmp = (tmp / K) + 1;
            tmp++;
          }
          if (ans > tmp) {
            ans = tmp;
          }
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
void dfs(int posx, int posy, int depth) {
  A[posy][posx] = 'S';
  if (depth == 0) {
    return;
  }
  if (0 <= posx - 1 && posx - 1 < W && 0 <= posy && posy < H) {
    if (A[posy][posx - 1] == '.') {
      dfs(posx - 1, posy, depth - 1);
    }
  }
  if (0 <= posx + 1 && posx + 1 < W && 0 <= posy && posy < H) {
    if (A[posy][posx + 1] == '.') {
      dfs(posx + 1, posy, depth - 1);
    }
  }
  if (0 <= posx && posx < W && 0 <= posy - 1 && posy - 1 < H) {
    if (A[posy - 1][posx] == '.') {
      dfs(posx, posy - 1, depth - 1);
    }
  }
  if (0 <= posx && posx < W && 0 <= posy + 1 && posy + 1 < H) {
    if (A[posy + 1][posx] == '.') {
      dfs(posx, posy + 1, depth - 1);
    }
  }
}
int min(int a, int b) {
  if (a >= b) {
    return b;
  } else {
    return a;
  }
}
