#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
ll mod = static_cast<ll>(1e9 + 7);
std::ostream& endn(std::ostream& os) { return os.put(os.widen('\n')); }
template <class T>
constexpr int sz(const T& a) {
  return (int)a.ne();
}
template <class T>
constexpr T pow2(const T a) {
  return a * a;
}
template <class T, class... Args>
constexpr T mins(T& a, Args... args) {
  return a = min<T>({a, args...});
}
template <class T, class... Args>
constexpr T maxs(T& a, Args... args) {
  return a = max<T>({a, args...});
}
template <class A, int N, class T>
void fillArray(A (&array)[N], const T& val) {
  std::fill((T*)array, (T*)(array + N), val);
}
int h, w, k;
char a[805][805];
int d[805][805];
void solve() {
  cin >> h >> w >> k;
  int sx = 0, sy = 0;
  for (int i = 0; i < (int)(h); ++i)
    for (int j = 0; j < (int)(w); ++j) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
        a[i][j] = '.';
      }
    }
  fillArray(d, iinf);
  d[sx][sy] = 0;
  queue<pair<int, int>> que;
  que.emplace(sx, sy);
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  while (!que.empty()) {
    auto pos = que.front();
    que.pop();
    if (pos.first == 0 || pos.first == h - 1 || pos.second == 0 ||
        pos.second == w - 1)
      continue;
    for (int i = 0; i < (int)(4); ++i) {
      int nx = pos.first + dx[i];
      int ny = pos.second + dy[i];
      if (a[nx][ny] == '#') continue;
      if (d[pos.first][pos.second] + 1 < d[nx][ny]) {
        d[nx][ny] = d[pos.first][pos.second] + 1;
        que.emplace(nx, ny);
      }
    }
  }
  int ans = iinf;
  for (int i = 0; i < (int)(h); ++i)
    for (int j = 0; j < (int)(w); ++j)
      if (a[i][i] == '.') {
        mins(ans,
             d[i][j] / k + 1 + (min({i, h - i - 1, j, w - j - 1}) + k - 1) / k);
      }
  cout << ans << endn;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  cout << flush;
  return 0;
}
