#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T& x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}
template <typename T, typename... U>
inline void read(T& x, U&... y) {
  read(x), read(y...);
}
int n, m, K;
bool vis[810][810];
char s[810][810];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
vector<pair<int, int> > GG;
struct Data {
  int x, y, d;
  Data() {}
  Data(int x, int y, int d) : x(x), y(y), d(d) {}
};
int main() {
  read(n, m, K);
  for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
  int X, Y;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i][j] == 'S') {
        X = i, Y = j;
      }
    }
  }
  if (X == 1 || X == n || Y == 1 || Y == m) return puts("0"), 0;
  queue<Data> Q;
  Q.push(Data(X, Y, 0));
  while (!Q.empty()) {
    Data A = Q.front();
    Q.pop();
    int x = A.x, y = A.y, d = A.d;
    GG.push_back(make_pair(x, y));
    vis[x][y] = true;
    if (d == K) continue;
    for (int i = 0; i < 4; ++i) {
      int tx = x + dx[i], ty = y + dy[i];
      if (tx < 1 || ty < 1 || tx > n || ty > m || vis[tx][ty] ||
          s[tx][ty] == '#')
        continue;
      Q.push(Data(tx, ty, d + 1));
    }
  }
  int res = 1e9;
  for (auto t : GG) {
    int d = min(min(t.first - 1, n - t.first), min(t.second - 1, m - t.second));
    int g = d / K;
    if (d % K) ++g;
    res = min(res, g);
  }
  printf("%d\n", res + 1);
  return 0;
}
