#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T>
void pv(T a, T b) {
  for (T i = a; i != b; ++i) cout << (*i) << " ";
  cout << endl;
}
template <class T>
void chmin(T& a, const T& b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}
int nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}
int H, W, K;
string s[810];
int memo[810][810];
const int INF = 1001001001;
const int dr[] = {0, 1, 0, -1};
const int dc[] = {1, 0, -1, 0};
void bfs(int r, int c, int walk) {
  for (int i = 0, _n = (int)(H); i < _n; ++i)
    for (int j = 0, _n = (int)(W); j < _n; ++j) memo[i][j] = INF;
  memo[r][c] = 0;
  queue<int> q;
  q.push(r);
  q.push(c);
  while (q.size() > 0) {
    r = q.front();
    q.pop();
    c = q.front();
    q.pop();
    if (memo[r][c] == walk) break;
    for (int i = 0; i < 4; i++) {
      int nr = r + dr[i];
      int nc = c + dc[i];
      if (0 <= nr && nr < H && 0 <= nc && nc < H && s[nr][nc] == '.' &&
          memo[nr][nc] == INF) {
        memo[nr][nc] = memo[r][c] + 1;
        q.push(nr);
        q.push(nc);
      }
    }
  }
}
int ceil(int r, int x) { return (r + x - 1) / x; }
int f(int r, int c) {
  if (r == 0 || c == 0 || r == H - 1 || c == W - 1) return 0;
  return min({ceil(r, K), ceil(H - 1 - r, K), ceil(c, K), ceil(W - 1 - c, K)});
}
int main2() {
  cin >> H >> W >> K;
  for (int i = 0, _n = (int)(H); i < _n; ++i) cin >> s[i];
  int si = 0, sj = 0;
  for (int i = 0, _n = (int)(H); i < _n; ++i)
    for (int j = 0, _n = (int)(W); j < _n; ++j)
      if (s[i][j] == 'S') {
        si = i;
        sj = j;
        s[i][j] = '.';
        break;
      }
  bfs(si, sj, K);
  int ans = INF;
  for (int i = 0, _n = (int)(H); i < _n; ++i)
    for (int j = 0, _n = (int)(W); j < _n; ++j) {
      if (memo[i][j] <= K) {
        chmin(ans, f(i, j) + 1);
      }
    }
  cout << ans << endl;
  return 0;
}
int main() {
  for (; !cin.eof(); cin >> ws) main2();
  return 0;
}
