#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
template <typename T>
using uset = unordered_set<T>;
template <typename T1, typename T2>
using umap = unordered_map<T1, T2>;
constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;
template <typename T>
T gcd(T a, T b) {
  return (b ? gcd(b, a % b) : a);
}
template <typename T>
T lcm(T a, T b) {
  return (a / gcd(a, b) * b);
}
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
int H, W, K;
string A[805];
int mc[805][805];
int main() {
  queue<Pi> que;
  fill_n(*mc, 805 * 805, INF);
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    cin >> A[i];
    for (int j = 0; j < W; j++) {
      if (A[i][j] == 'S') {
        mc[i][j] = 0;
        que.emplace(i, j);
      }
    }
  }
  while (!que.empty()) {
    int y, x;
    tie(y, x) = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || nx < 0 || H <= ny || W <= nx || A[ny][nx] == '#' ||
          mc[ny][nx] != INF)
        continue;
      mc[ny][nx] = mc[y][x] + 1;
      que.emplace(ny, nx);
    }
  }
  int minv = INF;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (A[i][j] != '#') {
        int dist = min(min(H - 1 - i, i), min(W - 1 - j, j));
        chmin(minv, mc[i][j] / K + (dist + K - 1) / K + 1);
      }
    }
  }
  cout << minv << endl;
  return (0);
}
