#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ll INF = 1e16;
const ll MOD = 1e9 + 7;
int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  ll sy = 0, sx = 0;
  for (ll i = 0; i < h; i++) {
    cin >> a[i];
    for (ll j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        sy = i;
        sx = j;
      }
    }
  }
  struct data {
    ll y, x, d;
  };
  queue<data> que;
  que.push({sy, sx, k});
  const ll dy[4] = {-1, 0, 1, 0};
  const ll dx[4] = {0, 1, 0, -1};
  vector<vector<bool>> used(h, vector<bool>(w));
  used[sy][sx] = true;
  while (!que.empty()) {
    auto t = que.front();
    que.pop();
    ll y = t.y, x = t.x, d = t.d;
    if (y == 0 || y == h - 1 || x == 0 || x == w - 1) {
      cout << 1 << endl;
      return 0;
    }
    for (ll i = 0; i < 4; i++) {
      ll yy = y + dy[i], xx = x + dx[i];
      if (a[yy][xx] != '#' && !used[yy][xx] && d > 0) {
        que.push({yy, xx, d - 1});
        used[yy][xx] = true;
      }
    }
  }
  ll ans = INF;
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      if (used[i][j]) {
        ans = min(ans, 1 + ((min(i, j) % k == 0) ? min(i, j) / k
                                                 : min(i, j) / k + 1));
      }
    }
  }
  cout << ans << endl;
}
