#include <bits/stdc++.h>
using namespace std;
const int N = 809;
const int M = N * N;
const int H = M << 3;
const int INF = 1e9;
const double EPS = 0.5;
char pat[N][N];
int n, m, K, S, dis[M], head[M], to[H], nxt[H], done[M];
priority_queue<pair<int, int> > que;
inline int read() {
  char c = getchar();
  int f = 1, ret = 0;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return ret * f;
}
inline int id(int x, int y) { return (y - 1) * m + x; }
inline void AddEdge(int u, int v) {
  static int E = 1;
  to[++E] = v;
  nxt[E] = head[u];
  head[u] = E;
  to[++E] = u;
  nxt[E] = head[v];
  head[v] = E;
}
inline void Dijkstra() {
  memset(dis, 60, sizeof(dis));
  dis[S] = 0;
  que.push(make_pair(0, S));
  while (!que.empty()) {
    int w = que.top().second;
    que.pop();
    if (done[w])
      continue;
    else
      done[w] = 1;
    for (int i = head[w]; i; i = nxt[i]) {
      if (dis[to[i]] > dis[w] + 1) {
        dis[to[i]] = dis[w] + 1;
        que.push(make_pair(-dis[to[i]], to[i]));
      }
    }
  }
}
int main() {
  n = read();
  m = read();
  K = read();
  for (int i = 1; i <= n; i++) {
    scanf("%s", pat[i] + 1);
    for (int j = 1; j <= m; j++) {
      if (pat[i][j] != '#') {
        if (i > 1 && pat[i - 1][j] != '#') {
          AddEdge(id(i, j), id(i - 1, j));
        }
        if (j > 1 && pat[i][j - 1] != '#') {
          AddEdge(id(i, j), id(i, j - 1));
        }
      }
      if (pat[i][j] == 'S') {
        S = id(i, j);
      }
    }
  }
  Dijkstra();
  int ans = INF;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (pat[i][j] != '#') {
        if (dis[id(i, j)] <= K) {
          int tmp = EPS + 1.0 +
                    ceil(min(min(i - 1, j - 1), min(n - i, m - j)) / (double)K);
          ans = min(ans, tmp);
        }
      }
    }
  }
  cout << ans;
  return 0;
}
