#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T>
using PQ = priority_queue<T>;
template <class T>
using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
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);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}
int h, w, k;
int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};
inline bool able(int i, int j) { return i >= 0 and j >= 0 and i < h and j < w; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> k;
  vs s(h);
  for (int i = 0; i < h; ++i) cin >> s[i];
  P start;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (s[i][j] == 'S') start = P(i, j);
  queue<P> q1;
  PQG<tuple<int, int, int>> q;
  q1.push(start);
  vvi check(h, vi(w, INF));
  check[start.first][start.second] = 0;
  while (!q1.empty()) {
    int i, j;
    tie(i, j) = q1.front();
    q1.pop();
    if (check[i][j] == k) continue;
    for (int h = 0; h < 4; ++h) {
      int ni = i + di[h];
      int nj = j + dj[h];
      if (able(ni, nj) and s[ni][nj] == '.' and check[ni][nj] == INF) {
        q1.emplace(ni, nj);
        check[ni][nj] = check[i][j] + 1;
      }
    }
    q.emplace(1, i, j);
  }
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j) {
      if (check[i][j] != INF) check[i][j] = 1;
    }
  while (!q.empty()) {
    int now, i, j;
    tie(now, i, j) = q.top();
    q.pop();
    if (now != check[i][j]) continue;
    for (int h = 0; h < 4; ++h) {
      int ni = i + di[h];
      int nj = j + dj[h];
      if (able(ni, nj) and chmin(check[ni][nj], check[i][j] + 1)) {
        q.emplace(now + 1, ni, nj);
      }
    }
  }
  int mi = INF;
  for (int i = 0; i < h; ++i) chmin(mi, min(check[i][0], check[i][w - 1]));
  for (int j = 0; j < w; ++j) chmin(mi, min(check[0][j], check[h - 1][j]));
  cout << 1 + (mi - 2 + k) / k << endl;
}
