#include <bits/stdc++.h>
using namespace std;
const int_fast32_t inf = 1e9 + 7;
const int_fast32_t MOD = 1000000007;
const int_fast64_t llinf = 1e18;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int_fast32_t dy[4] = {1, 0, -1, 0};
int_fast32_t dx[4] = {0, 1, 0, -1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int_fast32_t h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  for (int_fast64_t i = 0; i < (h); ++i) cin >> a[i];
  int_fast32_t sy = -1, sx = -1;
  for (int_fast64_t i = 0; i < (h); ++i)
    for (int_fast64_t j = 0; j < (w); ++j)
      if (a[i][j] == 'S') tie(sy, sx) = tie(i, j);
  vector<vector<int_fast32_t>> dist(h, vector<int_fast32_t>(w, inf));
  dist[sy][sx] = 0;
  priority_queue<pair<int_fast32_t, pair<int_fast32_t, int_fast32_t>>> que;
  que.emplace(0, pair<int_fast32_t, int_fast32_t>(sy, sx));
  while (!que.empty()) {
    int_fast32_t d, y, x;
    d = que.top().first;
    tie(y, x) = que.top().second;
    que.pop();
    if (dist[y][x] < d) continue;
    for (int_fast64_t i = 0; i < (4); ++i) {
      int_fast32_t ny = y + dy[i];
      int_fast32_t nx = x + dx[i];
      if (ny < 0 || h <= ny) continue;
      if (nx < 0 || w <= nx) continue;
      if (d >= k) continue;
      if (a[ny][nx] == '#') continue;
      if (chmin(dist[ny][nx], dist[y][x] + 1))
        que.emplace(dist[ny][nx], pair<int_fast32_t, int_fast32_t>(ny, nx));
    }
  }
  for (int_fast64_t i = 0; i < (h); ++i)
    for (int_fast64_t j = 0; j < (w); ++j) {
      if (dist[i][j] <= k) {
        dist[i][j] = 0;
        que.emplace(0, pair<int_fast32_t, int_fast32_t>(i, j));
      }
    }
  while (!que.empty()) {
    int_fast32_t d, y, x;
    d = que.top().first;
    tie(y, x) = que.top().second;
    que.pop();
    if (dist[y][x] < d) continue;
    for (int_fast64_t i = 0; i < (4); ++i) {
      int_fast32_t ny = y + dy[i];
      int_fast32_t nx = x + dx[i];
      if (ny < 0 || h <= ny) continue;
      if (nx < 0 || w <= nx) continue;
      if (chmin(dist[ny][nx], dist[y][x] + 1))
        que.emplace(dist[ny][nx], pair<int_fast32_t, int_fast32_t>(ny, nx));
    }
  }
  int_fast32_t ans = inf;
  for (int_fast64_t i = 0; i < (h); ++i) {
    chmin(ans, (dist[i][0] + k - 1) / k + 1);
    chmin(ans, (dist[i][w - 1] + k - 1) / k + 1);
  }
  for (int_fast64_t j = 0; j < (w); ++j) {
    chmin(ans, (dist[0][j] + k - 1) / k + 1);
    chmin(ans, (dist[h - 1][j] + k - 1) / k + 1);
  }
  cout << (ans) << "\n";
  return 0;
}
