#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
const long long MAX_N = 1e5;
long long divup(long long x, long long y) {
  if (x % y == 0)
    return x / y;
  else
    return x / y + 1;
}
long long dx[4] = {1, 0, -1, 0};
long long dy[4] = {0, 1, 0, -1};
signed main() {
  long long H, W, K;
  cin >> H >> W >> K;
  char maze[H][W];
  long long depth[H][W];
  long long sx, sy;
  for (long long i = (0); i < (H); ++i)
    for (long long j = (0); j < (W); ++j) {
      cin >> maze[i][j];
      if (maze[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  for (long long i = (0); i < (H); ++i)
    for (long long j = (0); j < (W); ++j) depth[i][j] = INF;
  queue<pair<pair<long long, long long>, long long> > q;
  long long ans = INF;
  q.push({{sx, sy}, 0});
  while (q.size()) {
    pair<pair<long long, long long>, long long> t = q.front();
    q.pop();
    long long nx = t.first.first;
    long long ny = t.first.second;
    long long dep = t.second;
    depth[nx][ny] = dep;
    chmin(ans, 1 + divup(min(min(H - 1 - nx, nx), min(W - 1 - ny, ny)), K));
    if (dep == K) continue;
    for (long long i = (0); i < (4); ++i) {
      long long x = nx + dx[i];
      long long y = nx + dy[i];
      if (x >= 0 && x < H && y >= 0 && y < W && maze[x][y] == '.' &&
          depth[x][y] == INF) {
        if (x == 0 || x == H - 1 || y == 0 || y == W - 1) {
          cout << 1 << endl;
          return 0;
        }
        q.push({{x, y}, dep + 1});
      }
    }
  }
  cout << ans << endl;
}
