#include <bits/stdc++.h>
struct Position {
  int h, w, cost, del;
  Position(int h, int w, int cost, int del)
      : h(h), w(w), cost(cost), del(del){};
  bool operator<(const Position& another) const { return cost < another.cost; }
  bool operator>(const Position& another) const { return cost > another.cost; }
};
int main() {
  int H, W, K, sh, sw, DP[800][800];
  char A[801][800];
  scanf("%d %d %d", &H, &W, &K);
  std::priority_queue<Position, std::vector<Position>, std::greater<Position>>
      queue;
  for (int i = 0; i < H; i++) {
    scanf("%s", A[i]);
    for (int j = 0; j < W; j++) {
      DP[i][j] = 10000000;
      if (A[i][j] == 'S') sh = i, sw = j, A[i][j] = '.';
    }
  }
  queue.emplace(sh, sw, 0, 0);
  DP[sh][sw] = 0;
  while (!queue.empty()) {
    Position cur_pos = queue.top();
    queue.pop();
    if (cur_pos.h != 0 && A[cur_pos.h - 1][cur_pos.w] == '.' &&
        DP[cur_pos.h - 1][cur_pos.w] > (cur_pos.cost + 1)) {
      queue.emplace(cur_pos.h - 1, cur_pos.w, cur_pos.cost + 1, 0);
      DP[cur_pos.h - 1][cur_pos.w] = cur_pos.cost + 1;
    }
    if (cur_pos.h != H - 1 && A[cur_pos.h + 1][cur_pos.w] == '.' &&
        DP[cur_pos.h + 1][cur_pos.w] > (cur_pos.cost + 1)) {
      queue.emplace(cur_pos.h + 1, cur_pos.w, cur_pos.cost + 1, 0);
      DP[cur_pos.h + 1][cur_pos.w] = cur_pos.cost + 1;
    }
    if (cur_pos.w != 0 && A[cur_pos.h][cur_pos.w - 1] == '.' &&
        DP[cur_pos.h][cur_pos.w - 1] > (cur_pos.cost + 1)) {
      queue.emplace(cur_pos.h, cur_pos.w - 1, cur_pos.cost + 1, 0);
      DP[cur_pos.h][cur_pos.w - 1] = cur_pos.cost + 1;
    }
    if (cur_pos.w != W - 1 && A[cur_pos.h][cur_pos.w + 1] == '.' &&
        DP[cur_pos.h][cur_pos.w + 1] > (cur_pos.cost + 1)) {
      queue.emplace(cur_pos.h, cur_pos.w + 1, cur_pos.cost + 1, 0);
      DP[cur_pos.h][cur_pos.w + 1] = cur_pos.cost + 1;
    }
  }
  bool s = false;
  for (int i = 0; i < H; i++) {
    s |= DP[i][0] != 10000000;
    s |= DP[i][W - 1] != 10000000;
  }
  for (int i = 0; i < W; i++) {
    s |= DP[0][i] != 10000000;
    s |= DP[H - 1][i] != 10000000;
  }
  if (s)
    printf("%d\n", 1);
  else {
    int distance = 800;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        if (DP[i][j] != 10000000)
          distance = std::min(
              distance,
              std::min(std::min(i, j), std::min(H - 1 - i, W - 1 - j)));
    printf("%d\n", (distance + K - 1) / K + 1);
  }
}
