#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class S, class T>
istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.first >> p.second;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << '{' << p.first << "," << p.second << '}';
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  for (int i = 0; i < int(v.size()); ++i)
    os << v[i] << (i == int(v.size() - 1) ? ']' : ',');
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  cin >> a;
  int si, sj;
  for (int i = 0; i < int(h); ++i)
    for (int j = 0; j < int(w); ++j)
      if (a[i][j] == 'S') {
        si = i;
        sj = j;
      }
  vector<vector<bool> > seen(h, vector<bool>(w));
  queue<P> q;
  seen[si][sj] = true;
  q.emplace(si, sj);
  while (!q.empty()) {
    auto [i, j] = q.front();
    q.pop();
    for (int K = 0; K < int(4); ++K) {
      int ni = i + dx[K], nj = j + dy[K];
      if (ni < 0 or ni >= h or nj < 0 or nj >= w) continue;
      if (a[ni][nj] == '#') continue;
      if (seen[ni][nj]) continue;
      seen[ni][nj] = true;
      q.emplace(ni, nj);
    }
  }
  int mn = inf;
  for (int i = 0; i < int(h); ++i)
    for (int j = 0; j < int(w); ++j)
      if (seen[i][j]) {
        chmin(mn, min({i, j, h - 1 - i, w - 1 - j}));
      }
  cout << (mn + k - 1) / k + 1 << endl;
}
