#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0;
  bool t = false;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') t = true, ch = getchar();
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
  return t ? -x : x;
}
int ans, n, m, K, sx, sy;
char g[808][808];
int Calc(int x, int y) { return min(min(x - 1, n - x), min(y - 1, m - y)); }
int dis[808][808];
int d[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
void BFS(int sx, int sy) {
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) dis[i][j] = 1e9;
  dis[sx][sy] = 0;
  ans = 1e9;
  queue<int> Qx, Qy;
  Qx.push(sx), Qy.push(sy);
  while (!Qx.empty()) {
    int x = Qx.front(), y = Qy.front();
    Qx.pop();
    Qy.pop();
    ans = min(ans, (Calc(x, y) + K - 1) / K);
    if (dis[x][y] >= K) continue;
    for (int i = 0; i < 4; ++i) {
      int xx = x + d[i][0], yy = y + d[i][1];
      if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
      if (g[xx][yy] == '#') continue;
      dis[xx][yy] = dis[x][y] + 1;
      Qx.push(xx);
      Qy.push(yy);
    }
  }
}
int main() {
  n = read();
  m = read();
  K = read();
  for (int i = 1; i <= n; ++i) scanf("%s", g[i] + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (g[i][j] == 'S') sx = i, sy = j;
  BFS(sx, sy);
  printf("%d\n", ans + 1);
  return 0;
}
