#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main() {
  int H;
  scanf("%d", &H);
  getchar();
  ;
  int W;
  scanf("%d", &W);
  getchar();
  ;
  int K;
  scanf("%d", &K);
  getchar();
  ;
  vector<vector<char>> board(H, vector<char>(W));
  using coordinate = pair<int, int>;
  coordinate start;
  for (int y = 0; y < H; ++y) {
    for (int x = 0; x < W; ++x) {
      board[y][x] = getchar();
      if (board[y][x] == 'S') {
        start = make_pair(x, y);
      }
    }
    getchar();
  }
  queue<tuple<coordinate, int>> q;
  q.emplace(start, 0);
  auto add_to_queue = [&](int x, int y, int times) {
    if (x < 0 || x >= W || y < 0 || y >= H || board[y][x] == '#') return;
    q.emplace(make_pair(x, y), times);
  };
  int distance = INT_MAX;
  while (!q.empty()) {
    int x = get<0>(q.front()).first;
    int y = get<0>(q.front()).second;
    int times = get<1>(q.front());
    q.pop();
    board[y][x] = '#';
    distance = min(distance, min(min(x, W - 1 - x), min(y, H - 1 - y)));
    if (times < K) {
      add_to_queue(x - 1, y, times + 1);
      add_to_queue(x + 1, y, times + 1);
      add_to_queue(x, y - 1, times + 1);
      add_to_queue(x, y + 1, times + 1);
    }
  }
  if (distance == 0) {
    printf("%d\n", 1);
  } else {
    printf("%d\n", 2 + (distance - 1) / K);
  }
}
