#include <bits/stdc++.h>
template <class T>
inline T chmax(T& a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T>
inline T chmin(T& a, const T b) {
  return a = (a > b) ? b : a;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = acos(-1);
using namespace std;
ll dx[] = {0, 0, 1, -1};
ll dy[] = {1, -1, 0, 0};
signed main() {
  ll H, W, K;
  cin >> H >> W >> K;
  vector<string> s(H);
  for (ll i = 0; i < ll(H); ++i) cin >> s[i];
  pair<ll, ll> start;
  for (ll i = 0; i < ll(H); ++i)
    for (ll j = 0; j < ll(W); ++j)
      if (s[i][j] == 'S') start = make_pair(i, j);
  queue<pair<ll, ll>> q;
  vector<vector<ll>> dist(H, vector<ll>(W, INF));
  dist[start.first][start.second] = 0;
  q.push(start);
  ll ans = INF;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    chmin(ans,
          min(min(p.first, p.second), min(H - p.first - 1, W - p.second - 1)));
    for (ll i = 0; i < ll(4); ++i) {
      ll x = p.first + dx[i];
      ll y = p.second + dy[i];
      if ((x < 0 || H <= x) || (y < 0 || W <= y)) continue;
      if (s[x][y] == '#') continue;
      ll d = dist[p.first][p.second];
      if (dist[x][y] <= d) continue;
      dist[x][y] = d;
      q.push(make_pair(x, y));
    }
  }
  cout << (ans + K - 1) / K + 1 << endl;
  return 0;
}
