#include <bits/stdc++.h>
using namespace std;
long long h, w, k;
string s[1919];
bool used[1919][1919];
long long dy[4] = {1, 0, -1, 0};
long long dx[4] = {0, 1, 0, -1};
long long dfs(pair<long long, long long> b, long long d) {
  if (0 <= b.first && b.first < h && 0 <= b.second && b.second < w &&
      s[b.first][b.second] != '#' && !used[b.first][b.second]) {
    used[b.first][b.second] = true;
    if (d <= k) {
      long long m =
          (min(min(b.first, h - b.first - 1), min(b.second, w - b.second - 1)) +
           k - 1) /
          k;
      for (long long i = 0; i < (4); i++) {
        m = min(m, dfs(make_pair(b.first + dy[i], b.second + dx[i]), d + 1));
      }
      return m;
    }
  }
  return 1000000007LL;
}
signed main() {
  cin >> h >> w >> k;
  for (long long i = 0; i < (h); i++) {
    cin >> s[i];
  }
  pair<long long, long long> a;
  for (long long i = 0; i < (h); i++) {
    for (long long j = 0; j < (w); j++) {
      if (s[i][j] == 'S') {
        a = make_pair(i, j);
      }
    }
  }
  for (long long i = 0; i < (h); i++) {
    for (long long j = 0; j < (w); j++) {
      used[i][j] = false;
    }
  }
  cout << dfs(a, 0) + 1 << endl;
}
