#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();
struct Pos {
  ll x, y;
};
void solve() {
  ll H, W, K;
  cin >> H >> W >> K;
  int cell[801][801];
  Pos start;
  for (ll i = 0; i < H; i++) {
    string s;
    cin >> s;
    for (ll j = 0; j < W; j++) {
      if (s[j] == '#') {
        cell[i][j] = 1;
      } else {
        cell[i][j] = 0;
        if (s[j] == 'S') {
          start = {i, j};
        }
      }
    }
  }
  ll dist[801][801];
  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      dist[i][j] = L_INF;
    }
  }
  constexpr Pos step[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  using Tup = tuple<ll, ll, ll>;
  queue<Tup> q1, q2;
  q1.emplace(start.x, start.y, 0);
  dist[start.x][start.y] = 0;
  while (!q1.empty()) {
    auto [x, y, d] = q1.front();
    q1.pop();
    bool is_terminal = true;
    if (d + 1 <= K) {
      for (ll i = 0; i < 4; i++) {
        ll xn = x + step[i].x;
        ll yn = y + step[i].y;
        if (!(xn >= 0 && xn < H && yn >= 0 && yn < W)) continue;
        if (cell[xn][yn] == 1) continue;
        if (dist[xn][yn] != L_INF) continue;
        q1.emplace(xn, yn, d + 1);
        dist[xn][yn] = d + 1;
        is_terminal = false;
      }
    }
    if (is_terminal) {
      q2.emplace(x, y, K);
    }
  }
  while (!q2.empty()) {
    auto [x, y, d] = q2.front();
    q2.pop();
    for (ll i = 0; i < 4; i++) {
      ll xn = x + step[i].x;
      ll yn = y + step[i].y;
      if (!(xn >= 0 && xn < H && yn >= 0 && yn < W)) continue;
      if (dist[xn][yn] != L_INF) continue;
      q2.emplace(xn, yn, d + 1);
      dist[xn][yn] = d + 1;
    }
  }
  ll ans = L_INF;
  for (ll i = 0; i < H; i++) {
    ans = min(ans, (dist[i][0] + K - 1) / K);
    ans = min(ans, (dist[i][W - 1] + K - 1) / K);
  }
  for (ll j = 0; j < W; j++) {
    ans = min(ans, (dist[0][j] + K - 1) / K);
    ans = min(ans, (dist[H - 1][j] + K - 1) / K);
  }
  cout << ans << "\n";
}
int main() {
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
  solve();
  cout << flush;
  return 0;
}
