#include <bits/stdc++.h>
#pragma warning(disable : 4786)
#pragma warning(disable : 4996)
using namespace std;
int H, W, K;
char board[802][802];
int dist[802][802];
int dr[] = {0, 0, 1, -1};
int dc[] = {1, -1, 0, 0};
int main() {
  scanf("%d %d %d", &H, &W, &K);
  for (int i = 0; i < H; i++) scanf("%s", board[i]);
  pair<int, int> start = pair<int, int>(-1, -1);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (board[i][j] == 'S') {
        start = pair<int, int>(i, j);
        board[i][j] = '.';
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dist[i][j] = -1;
    }
  }
  dist[start.first][start.second] = 0;
  queue<pair<int, int> > Q;
  Q.push(start);
  while (!Q.empty()) {
    pair<int, int> u = Q.front();
    Q.pop();
    if (dist[u.first][u.second] == K) continue;
    for (int d = 0; d < 4; d++) {
      pair<int, int> end;
      end.first = u.first + dr[d];
      end.second = u.second + dc[d];
      if (!(((0) <= (end.first) && (end.first) <= (H - 1)) &&
            ((0) <= (end.second) && (end.second) <= (W - 1))))
        continue;
      if (board[end.first][end.second] == '#') continue;
      if (dist[end.first][end.second] == -1) {
        dist[end.first][end.second] = dist[u.first][u.second] + 1;
        Q.push(end);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i == 0 || i == H - 1 || j == 0 || j == W - 1) {
        if (dist[i][j] != -1) {
          ans = 1;
        }
      }
    }
  }
  if (ans) {
    printf("1\n");
    return 0;
  }
  ans = 1000000000;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (dist[i][j] == -1) continue;
      int d = ((i) < (j) ? (i) : (j));
      d = ((d) < (H - i) ? (d) : (H - i));
      d = ((d) < (W - j) ? (d) : (W - j));
      int now = (d + K - 1) / K;
      ans = ((ans) < (now + 1) ? (ans) : (now + 1));
    }
  }
  printf("%d\n", ans);
  return 0;
}
