#include <bits/stdc++.h>
using namespace std;
int dh[] = {1, -1, 0, 0};
int dw[] = {0, 0, 1, -1};
int main() {
  int h, w;
  int k;
  cin >> h >> w >> k;
  vector<string> a(h);
  for (int i = 0; i < h; i++) cin >> a[i];
  pair<int, int> s;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        s.first = i;
        s.second = j;
        i = h;
        j = w;
      }
    }
  }
  auto isrange = [&h, &w](int i, int j) {
    return 0 <= i && i < h && 0 <= j && j < w;
  };
  queue<pair<pair<int, int>, int>> que;
  vector<vector<int>> reach(h, vector<int>(w, 0));
  que.push(make_pair(s, k));
  while (!que.empty()) {
    auto st = que.front();
    que.pop();
    auto p = st.first;
    int step = st.second;
    if (reach[p.first][p.second]) continue;
    reach[p.first][p.second] = true;
    if (step == 0) continue;
    for (int i = 0; i < 4; i++) {
      int toh = p.first + dh[i];
      int tow = p.second + dw[i];
      if (isrange(toh, tow) && !reach[toh][tow] && a[toh][tow] != '#') {
        reach[toh][tow] = 1;
        que.push(make_pair(make_pair(toh, tow), step - 1));
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (reach[i][j]) que.push(make_pair(make_pair(i, j), k));
    }
  }
  while (!que.empty()) {
    auto st = que.front();
    que.pop();
    auto p = st.first;
    int score = st.second;
    if (p.first == 0 || p.first == h - 1 || p.second == 0 ||
        p.second == w - 1) {
      cout << score / k + (score % k != 0) << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      int toh = p.first + dh[i];
      int tow = p.second + dw[i];
      if (isrange(toh, tow) && !reach[toh][tow]) {
        reach[toh][tow] = 1;
        que.push(make_pair(make_pair(toh, tow), score + 1));
      }
    }
  }
  return 0;
}
