#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
const double EPS = 1e-10;
double zero(double d) { return abs(d) < EPS ? 0.0 : d; }
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  for (int i = 0; i < (int)(a.size()); i++) os << (i ? " " : "") << a[i];
  return os << ']';
}
string toString(int i) {
  stringstream ss;
  ss << i;
  return ss.str();
}
const int MOD = 1000000007;
long long fpow(long long a, long long k, int M) {
  long long res = 1ll;
  long long x = a;
  while (k != 0) {
    if ((k & 1) == 1) res = (res * x) % M;
    x = (x * x) % M;
    k >>= 1;
  }
  return res;
}
struct prepare {
  prepare() {
    cout.setf(ios::fixed, ios::floatfield);
    cout.precision(8);
    ios_base::sync_with_stdio(false);
  }
} _prepare;
vector<int> dr = {1, 0, -1, 0};
vector<int> dc = {0, -1, 0, 1};
int solve() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> fi(H);
  for (int i = 0; i < (int)(H); i++) {
    cin >> fi[i];
  }
  pair<int, int> start;
  int nearest = -1;
  for (int r = 0; r < (int)(H); r++) {
    for (int c = 0; c < (int)(W); c++) {
      if (fi[r][c] == 'S') {
        start = make_pair(r, c);
        nearest = min(min(r, H - r), min(c, W - c));
      }
    }
  }
  if (nearest == 0) {
    return 1;
  }
  priority_queue<pair<int, pair<int, int>>> que;
  que.push(make_pair(K - 1, start));
  vector<vector<int>> dp(H, vector<int>(W, false));
  while (!que.empty()) {
    auto f = que.top();
    que.pop();
    int d = f.first;
    int r = f.second.first;
    int c = f.second.second;
    chmin(nearest, min(min(r, H - r), min(c, W - c)));
    dp[r][c] = true;
    if (d == 0) continue;
    for (int i = 0; i < (int)(4); i++) {
      int nr = r + dr[i];
      int nc = c + dc[i];
      if (nr < 0 || H <= r || nc < 0 || W <= nc || fi[nr][nc] == '#' ||
          dp[nr][nc])
        continue;
      que.push(make_pair(d - 1, make_pair(nr, nc)));
    }
  }
  return 1 + (int)ceil((double)nearest / K);
}
int main() {
  auto res = solve();
  cout << res << endl;
  return 0;
}
