#include <bits/stdc++.h>
using namespace std;
template <class T, class S>
bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class S>
bool chmin(T &a, const S &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
long long gcd(long long n, long long m) { return (m ? gcd(m, n % m) : n); }
long long lcm(long long n, long long m) { return n / gcd(n, m) * m; }
long long h, w, k;
long long dx[4] = {0, 1, 0, -1};
long long dy[4] = {1, 0, -1, 0};
struct T {
  long long x, y, dis;
};
long long dist(long long x, long long y) {
  return min({x, h - 1 - x, y, w - 1 - y});
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  cin >> h >> w >> k;
  vector<vector<char>> a(h, vector<char>(w));
  queue<T> que;
  vector<vector<bool>> vis(h, vector<bool>(w));
  long long ans;
  for (long long i = 0; i < (h); i++) {
    for (long long j = 0; j < (w); j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        que.push({i, j, 0});
        ans = dist(i, j);
        vis[i][j] = true;
      }
    }
  }
  while (!que.empty()) {
    T p = que.front();
    que.pop();
    if (p.dis > k) continue;
    for (long long i = 0; i < (4); i++) {
      long long nx = p.x + dx[i];
      long long ny = p.y + dy[i];
      if (nx < 0 || h <= nx || ny < 0 || w <= ny || a[nx][ny] == '#' ||
          vis[nx][ny])
        continue;
      chmin(ans, dist(nx, ny));
      vis[nx][ny] = true;
      que.push({nx, ny, p.dis + 1});
    }
  }
  cout << (ans + k - 1) / k + 1 << '\n';
}
