#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B>
inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1ll << 29;
const long long MOD = 1000000007;
const double EPS = 1e-10;
int H, W, K;
int dp[801][801];
string maps[800];
int main() {
  cin >> H >> W >> K;
  for (int i = (0); i < (H); i++) cin >> maps[i];
  fill(dp[0], dp[H], INF);
  pair<int, int> sp;
  for (int i = (0); i < (H); i++)
    for (int j = (0); j < (W); j++)
      if (maps[i][j] == 'S') sp = pair<int, int>(i, j);
  queue<pair<int, int> > que;
  que.push(sp);
  dp[sp.first][sp.second] = 0;
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  while (!que.empty()) {
    int y = que.front().first;
    int x = que.front().second;
    que.pop();
    for (int i = (0); i < (4); i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (!(nx >= 0 && nx < W && ny >= 0 && ny < H)) continue;
      if (maps[ny][nx] == '#') continue;
      if (dp[ny][nx] <= dp[y][x]) continue;
      dp[ny][nx] = dp[y][x] + 1;
      que.push(pair<int, int>(ny, nx));
    }
  }
  int ans = INF;
  dp[sp.first][sp.second] = K;
  for (int i = (0); i < (H); i++)
    for (int j = (0); j < (W); j++)
      if (dp[i][j] != INF) {
        dp[i][j] = (dp[i][j] + K - 1) / K * K;
        chmin(ans, H - i + dp[i][j]);
        chmin(ans, W - j + dp[i][j]);
        chmin(ans, i + dp[i][j]);
        chmin(ans, j + dp[i][j]);
      }
  cout << (ans + K - 1) / K << endl;
  return 0;
}
