#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<long long>;
using pii = pair<long long, long long>;
const signed INF_ = 1001001001;
const long long INF = 1001001001001001001LL;
const long long DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
                DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0};
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto i = (begin(v)); i != (end(v)); ++i)
    os << *i << (i == end(v) - 1 ? "" : " ");
  return os;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto i = (begin(v)); i != (end(v)); ++i) is >> *i;
  return is;
}
template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
using heap = priority_queue<T, vector<T>, greater<T>>;
struct {
  template <class T>
  operator T() {
    T x;
    cin >> x;
    return x;
  }
} IN;
struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(15) << fixed;
  }
} before_main_function;
signed main() {
  long long H, W, K;
  cin >> H >> W >> K;
  vector<string> S(H + 2, string(W + 2, '#'));
  for (long long i = 0, ilen = (long long)(H); i < ilen; ++i) {
    string t;
    cin >> t;
    t = "#" + t + "#";
    S[i + 1] = t;
  }
  H += 2;
  W += 2;
  pii st;
  for (long long i = 0, ilen = (long long)(H); i < ilen; ++i) {
    for (long long j = 0, jlen = (long long)(W); j < jlen; ++j) {
      if (S[i][j] == 'S') st = {i, j};
    }
  }
  vector<vector<long long>> dist(H, vector<long long>(W, INF));
  queue<pii> q;
  q.push(st);
  dist[st.first][st.second] = 0;
  long long ans = INF;
  while (!q.empty()) {
    long long y = q.front().first;
    long long x = q.front().second;
    q.pop();
    if (S[y][x] == '#') {
      long long l1 = dist[y][x];
      long long l2 = min(min(y, x), min(H - y, W - x) - 1);
      if (l1 >= K) {
        chmin(ans, (l1 + l2 + K - 1) / K);
      } else {
        chmin(ans, 1 + (l2 + K - 1) / K);
      }
      continue;
    }
    for (long long dir = 0, dirlen = (long long)(4); dir < dirlen; ++dir) {
      long long yy = y + DY[dir];
      long long xx = x + DX[dir];
      if (((yy) < 0 || (xx) < 0 || (yy) >= (H) || (xx) >= (W))) continue;
      if (chmin(dist[yy][xx], dist[y][x] + 1)) {
        q.push({yy, xx});
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
