#include <bits/stdc++.h>
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using P = std::pair<ll, ll>;
using St = std::pair<P, ll>;
using St2 = std::pair<St, ll>;
const ll di[4] = {0, 1, 0, -1}, dj[4] = {1, 0, -1, 0};
ll H, W, K;
std::string A[1000];
std::queue<St> que;
std::queue<St2> que2;
bool used[1000][1000], used2[1000][1000];
ll ans = 1ll << 60;
void bfs1() {
  while (!que.empty()) {
    St st = que.front();
    ll i = st.first.first, j = st.first.second, dist = st.second;
    if (used[i][j]) break;
    used[i][j] = true;
    que.pop();
    if (!i || !j || i == H - 1 || j == W - 1) {
      chmin(ans, dist);
      break;
    }
    bool upd = false;
    for (int d = (0); d < (4); ++d) {
      ll ni = i + di[d], nj = j + dj[d];
      if (ni >= 0 && ni < H && nj >= 0 && nj < W && A[ni][nj] != '#' &&
          !used[ni][nj])
        que.push(St(P(ni, nj), dist + 1)), upd = true;
    }
    if (!upd) {
      que2.push(St2(St(P(i, j), 0), 1));
    }
  }
  return;
}
void bfs2() {
  while (!que2.empty()) {
    St2 st = que2.front();
    que2.pop();
    ll i = st.first.first.first, j = st.first.first.second,
       dist = st.first.second, org = st.second;
    if (!i || !j || i == H - 1 || j == W - 1) {
      chmin(ans, org + (dist + K - 1) / K);
      break;
    }
    for (int d = (0); d < (4); ++d) {
      ll ni = i + di[d], nj = j + dj[d];
      if (ni >= 0 && ni < H && nj >= 0 && nj < W && !used[ni][nj])
        que2.push(St2(St(P(ni, nj), dist + 1), org));
    }
  }
  return;
}
ll si, sj;
int main() {
  std::cin >> H >> W >> K;
  for (int i = (0); i < (H); ++i) {
    std::cin >> A[i];
    for (int j = (0); j < (W); ++j)
      if (A[i][j] == 'S') si = i, sj = j;
  }
  que.push(St(P(si, sj), 0));
  bfs1();
  bfs2();
  std::cout << ans << std::endl;
  return 0;
}
