#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll INF = (ll)1e18;
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};
template <typename T>
inline void pv(vector<T>& v) {
  for (ll i = 0, N = v.size(); i < N; i++)
    std::cout << v[i] << " \n"[i == N - 1];
}
template <typename T>
inline T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T>
inline T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
  return a > b && (a = b, true);
}
const ll H_MAX = 800;
const ll W_MAX = 800;
ll H, W, K;
vector<string> A;
void solve() {
  scanf("%lld%lld%lld", &H, &W, &K);
  A.assign(H, "");
  for (ll i = 0, i_length = (H); i < i_length; ++i) cin >> A[i];
  queue<vector<ll>> q;
  for (ll i = 0, i_length = (H); i < i_length; ++i)
    for (ll j = 0, j_length = (W); j < j_length; ++j) {
      if (A[i][j] == 'S') {
        q.push({i, j, 1});
        A[i][j] = '!';
        break;
      }
    }
  while (!q.empty()) {
    vector<ll> v = q.front();
    q.pop();
    ;
    if (v[2] == K + 1) break;
    for (ll i = 0, i_length = (4); i < i_length; ++i) {
      ll nx = dx[i] + v[0];
      ll ny = dy[i] + v[1];
      if (A[nx][ny] == '#' || A[nx][ny] == '!') continue;
      if (nx == 0 || ny == 0 || nx == H - 1 || ny == W - 1) {
        std::cout << 1 << std::endl;
        return;
      }
      A[nx][ny] = '!';
      q.push({nx, ny, v[2] + 1});
    }
  };
  ll anst = INF;
  for (ll i = 0, i_length = (H); i < i_length; ++i)
    for (ll j = 0, j_length = (W); j < j_length; ++j) {
      if (A[i][j] != '!') continue;
      ll v[2] = {i, j};
      vector<ll> t;
      t.push_back(v[0]);
      t.push_back(H - v[0]);
      t.push_back(v[1]);
      t.push_back(W - v[1]);
      ll s = *min_element((t).begin(), (t).end());
      ;
      chmin(anst, s);
    }
  ll ans = 1 + (anst + K - 1) / K;
  printf("%lld\n", ans);
}
signed main() {
  solve();
  return 0;
}
