#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int main() {
  cout << fixed << setprecision(10);
  long long h, w, k;
  cin >> h >> w >> k;
  vector<vector<char>> a(h, vector<char>(w));
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) cin >> a[i][j];
  using P = pair<long long, long long>;
  auto isOutside = [&](P &p) {
    long long r, c;
    tie(r, c) = p;
    return r < 0 or r >= h or c < 0 or c >= w;
  };
  auto getDist = [&](P &p) {
    long long r, c;
    tie(r, c) = p;
    return min({r, h - 1 - r, c, w - 1 - c});
  };
  auto isWall = [&](P &p) {
    long long r, c;
    tie(r, c) = p;
    return a[r][c] == '#';
  };
  P st;
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      if (a[i][j] == 'S') st = make_pair(i, j);
    }
  }
  queue<P> q;
  q.push(st);
  set<P> canReach;
  long long d = LLONG_MAX;
  while (!q.empty()) {
    P cur = q.front();
    q.pop();
    if (canReach.count(cur)) continue;
    canReach.insert(cur);
    chmin(d, getDist(cur));
    for (long long i = 0; i < 4; i++) {
      P nxt = cur;
      nxt.first += dx[i];
      nxt.second += dy[i];
      if (isOutside(nxt)) continue;
      if (canReach.count(nxt)) continue;
      if (isWall(nxt)) continue;
      q.push(nxt);
    }
  }
  cout << 1 + (d + k - 1) / k << endl;
  return 0;
}
