#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
using i64 = int64_t;
void solve() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  int sy, sx;
  for (int i = (0); i < (H); ++i) {
    cin >> A[i];
    for (int j = (0); j < (W); ++j)
      if (A[i][j] == 'S') {
        sy = i;
        sx = j;
        break;
      }
  }
  vector<vector<int>> dist(H, vector<int>(W, inf));
  queue<pair<int, int>> que;
  vector<int> dy({0, -1, 0, 1}), dx({1, 0, -1, 0});
  que.push(make_pair(sy, sx));
  dist[sy][sx] = 0;
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    int y = p.first, x = p.second;
    for (int i = (0); i < (4); ++i) {
      int y_ = y + dy[i], x_ = x + dx[i];
      if (y_ < 0 or H <= y_ or x_ < 0 or W <= x_ or A[y_][x_] == '#') continue;
      if (dist[y_][x_] != inf) continue;
      que.push(make_pair(y_, x_));
      dist[y_][x_] = dist[y][x] + 1;
    }
  }
  vector<int> mi1(H, inf), ma1(H, -inf), mi2(W, inf), ma2(W, -inf);
  for (int i = (0); i < (H); ++i)
    for (int j = (0); j < (W); ++j)
      if (dist[i][j] <= K) {
        mi1[i] = min(mi1[i], j);
        ma1[i] = max(ma1[i], j);
      }
  for (int i = (0); i < (W); ++i)
    for (int j = (0); j < (H); ++j)
      if (dist[j][i] <= K) {
        mi2[i] = min(mi2[i], j);
        ma2[i] = max(ma2[i], j);
      }
  int ans = inf;
  for (int i = (0); i < (H); ++i) {
    int y = i, x = 0;
    for (int j = (0); j < (H); ++j) {
      if (mi1[j] == inf) continue;
      ans = min(ans, 1 + (abs(y - j) + abs(x - mi1[j]) + K - 1) / K);
    }
  }
  for (int i = (0); i < (H); ++i) {
    int y = i, x = W - 1;
    for (int j = (0); j < (H); ++j) {
      if (ma1[j] == -inf) continue;
      ans = min(ans, 1 + (abs(y - j) + abs(x - ma1[j]) + K - 1) / K);
    }
  }
  for (int i = (0); i < (W); ++i) {
    int y = 0, x = i;
    for (int j = (0); j < (W); ++j) {
      if (mi2[j] == inf) continue;
      ans = min(ans, 1 + (abs(y - mi2[j]) + abs(x - j) + K - 1) / K);
    }
  }
  for (int i = (0); i < (W); ++i) {
    int y = H - 1, x = i;
    for (int j = (0); j < (W); ++j) {
      if (ma2[j] == -inf) continue;
      ans = min(ans, 1 + (abs(y - mi2[j]) + abs(x - j) + K - 1) / K);
    }
  }
  cout << ans << endl;
}
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}
