#include <bits/stdc++.h>
using namespace std;
const int maxn = 800 + 10;
const int INF = 1e7;
int n, m, k;
inline int C(int x) { return (x / k) + (x % k != 0); }
inline int cost(int dist, int blocked) { return max(C(dist), C(blocked)); }
char c[maxn][maxn];
pair<int, int> dis[maxn][maxn];
struct PII {
  int dist, blocked, x, y;
  PII(int dist, int blocked, int x, int y)
      : dist(dist), blocked(blocked), x(x), y(y) {}
};
bool operator<(PII A, PII B) {
  return (cost(A.dist, A.blocked) < cost(B.dist, B.blocked));
}
bool mrk[maxn][maxn];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
void solve(int xini, int yini) {
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) dis[i][j] = pair<int, int>(INF, INF);
  dis[xini][yini] = pair<int, int>(0, 0);
  multiset<PII> Set;
  Set.insert(PII(0, 0, xini, yini));
  while (!Set.empty()) {
    int x = Set.begin()->x;
    int y = Set.begin()->y;
    int dist = Set.begin()->dist;
    int blocked = Set.begin()->blocked;
    Set.erase(Set.begin());
    if (mrk[x][y]) continue;
    mrk[x][y] = true;
    for (int it = 0; it < 4; ++it) {
      int X = x + dx[it];
      int Y = y + dy[it];
      if (X <= 0 || Y <= 0 || X > n || Y > m) continue;
      if (c[X][Y] == '#') {
        ;
        if (C(blocked + 1) > C(dist)) {
          ;
          if (cost(dis[X][Y].first, dis[X][Y].second) >
              cost((C(blocked + 1) + 1) * k, blocked + 1)) {
            dis[X][Y] = pair<int, int>((C(blocked + 1) + 1) * k, blocked + 1);
            Set.insert(PII((C(blocked + 1) + 1) * k, blocked + 1, X, Y));
          }
        } else {
          if (cost(dis[X][Y].first, dis[X][Y].second) >
              cost(dist + 1, blocked + 1)) {
            dis[X][Y] = pair<int, int>(dist + 1, blocked + 1);
            Set.insert(PII(dist + 1, blocked + 1, X, Y));
          }
        }
      } else {
        if (cost(dis[X][Y].first, dis[X][Y].second) > cost(dist + 1, blocked)) {
          dis[X][Y] = pair<int, int>(dist + 1, blocked);
          Set.insert(PII(dist + 1, blocked, X, Y));
        }
      }
    }
  }
}
int ans = INF;
void get() {
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j)
      ;
    ;
  }
  for (int i = 1; i <= n; ++i) {
    ans = min(ans, cost(dis[i][1].first, dis[i][1].second));
    ans = min(ans, cost(dis[i][m].first, dis[i][m].second));
  }
  for (int j = 1; j <= m; ++j) {
    ans = min(ans, cost(dis[1][j].first, dis[1][j].second));
    ans = min(ans, cost(dis[n][j].first, dis[n][j].second));
  }
}
int main() {
  scanf("%d %d %d", &n, &m, &k);
  int xini = -1;
  int yini = -1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      scanf(" %c", &c[i][j]);
      if (c[i][j] == 'S') {
        xini = i;
        yini = j;
      }
    }
  solve(xini, yini);
  get();
  printf("%d\n", ans);
  return 0;
}
