#include <bits/stdc++.h>
using namespace std;
int H, W, K;
char A[800][800];
bool used[800][800];
int dist[800][800];
bool isValid(pair<int, int> p) {
  if (p.first >= 0 && p.first < H && p.second >= 0 && p.second < W)
    return true;
  else
    return false;
}
void bfs(int d, int h, int w) {
  dist[h][w] = 0;
  queue<pair<int, int> > que;
  que.push(pair<int, int>(h, w));
  while (!que.empty()) {
    pair<int, int> top = que.front();
    int h_ = top.first;
    int w_ = top.second;
    que.pop();
    used[h_][w_] = true;
    if (dist[h_][w_] == K) continue;
    if (isValid(pair<int, int>(h - 1, w)) && A[h_ - 1][w_] == '.' &&
        !used[h_ - 1][w_]) {
      que.push(pair<int, int>(h_ - 1, w_));
      dist[h_ - 1][w_] = dist[h_][w_] + 1;
    }
    if (isValid(pair<int, int>(h_ + 1, w_)) && A[h_ + 1][w_] == '.' &&
        !used[h_ + 1][w_]) {
      que.push(pair<int, int>(h_ + 1, w_));
      dist[h_ + 1][w_] = dist[h_][w_] + 1;
    }
    if (isValid(pair<int, int>(h_, w_ - 1)) && A[h_][w_ - 1] == '.' &&
        !used[h_][w_ - 1]) {
      que.push(pair<int, int>(h, w - 1));
      dist[h_][w_ - 1] = dist[h_][w_] + 1;
    }
    if (isValid(pair<int, int>(h_, w_ + 1)) && A[h_][w_ + 1] == '.' &&
        !used[h_][w_ + 1]) {
      que.push(pair<int, int>(h_, w_ - 1));
      dist[h_][w_ + 1] = dist[h_][w_] + 1;
    }
  }
}
int main() {
  cin >> H >> W >> K;
  pair<int, int> s;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') s = pair<int, int>(i, j);
      used[i][j] = false;
    }
  }
  bfs(0, s.first, s.second);
  int min_dist = 800;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (used[i][j]) {
        min_dist = min(min_dist, min({i, j, H - i - 1, W - j - 1}));
      }
    }
  }
  cout << 1 + ((min_dist + K - 1) / K) << endl;
}
