#include <bits/stdc++.h>
char A[810][810];
int B[810][810] = {0};
int H, W, K;
void check(int i, int j, int k) {
  if (A[i][j] == '.' || A[i][j] == 'S') {
    B[i][j] = 1;
  } else {
    B[i][j] = -1;
  }
  if (k > 0 && B[i][j] == 1) {
    if (B[i + 1][j] == 0 && i + 1 < H) {
      check(i + 1, j, k - 1);
    }
    if (B[i - 1][j] == 0 && i - 1 >= 0) {
      check(i - 1, j, k - 1);
    }
    if (B[i][j + 1] == 0 && j + 1 < W) {
      check(i, j + 1, k - 1);
    }
    if (B[i][j - 1] == 0 && j - 1 >= 0) {
      check(i, j - 1, k - 1);
    }
  }
}
int main(void) {
  scanf("%d %d %d", &H, &W, &K);
  int i, j;
  int s_i, s_j;
  for (i = 0; i < H; i++) {
    scanf("%s", A[i]);
  }
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      if (A[i][j] == 'S') {
        s_i = i;
        s_j = j;
      }
    }
  }
  check(s_i, s_j, K);
  int up = s_i;
  int down = s_i;
  int left = s_j;
  int right = s_j;
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      if (B[i][j] == 1) {
        if (i < up) {
          up = i;
        }
        if (i > down) {
          down = i;
        }
        if (j < left) {
          left = j;
        }
        if (j > right) {
          right = j;
        }
      }
    }
  }
  int min = 800;
  if (up < min) {
    min = up;
  }
  if (H - down < min) {
    min = H - down;
  }
  if (left < min) {
    min = left;
  }
  if (W - right < min) {
    min = W - right;
  }
  int ans = 1;
  if (min % K == 0) {
    ans += min / K;
  } else {
    ans += (min / K + 1);
  }
  printf("%d\n", ans);
  return 0;
}
