#include <bits/stdc++.h>
#pragma target("avx")
using ll = int_fast64_t;
using P = std::pair<ll, ll>;
using PP = std::pair<ll, P>;
using V = std::vector<ll>;
template <typename T>
using pq = std::priority_queue<T>;
template <typename T>
using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
int main() {
  int h, w, k;
  scanf("%d %d %d", &h, &w, &k);
  int sx, sy;
  bool fie[h][w];
  getchar();
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      char c = getchar();
      if (c == 'S') sx = i, sy = j;
      fie[i][j] = (c == '#');
    }
  }
  ll ans = 1e18;
  std::queue<PP> que;
  que.emplace(0, P(sx, sy));
  while (!que.empty()) {
    ll d = que.front().first;
    P p = que.front().second;
    que.pop();
    fie[p.first][p.second] = true;
    ll cost = std::min({p.first, h - p.first, p.second, w - p.second});
    ans = std::min(ans, std::max(1l, (d + k - 1) / k) + (cost + k - 1) / k);
    if (cost == 0) continue;
    for (ll i = 0; i < 4; i++) {
      ll nx = p.first + dx[i], ny = p.second + dy[i];
      if (!fie[nx][ny]) que.emplace(d + 1, P(nx, ny));
    }
  }
  printf("%ld\n", ans);
  return 0;
}
