#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char mz[1005][1005];
int sx, sy;
int vis[1005][1005];
void dfs1(int x, int y, int d) {
  if (x < 1 || y < 1 || x > h || y > w) return;
  if (mz[x][y] == '#' || d > k || vis[x][y]) return;
  vis[x][y] = 1;
  dfs1(x + 1, y, d + 1);
  dfs1(x - 1, y, d + 1);
  dfs1(x, y + 1, d + 1);
  dfs1(x, y - 1, d + 1);
}
struct point {
  int x, y;
  point(int a, int b) {
    x = a;
    y = b;
  }
  point() {}
} q[1005 * 1005];
int head, tail;
int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
bool legal(int x, int y) { return 1 <= x && x <= h && 1 <= y && y <= w; }
int dis[1005][1005];
void bfs() {
  while (head != tail) {
    point tmp = q[head++];
    for (int k = 0; k < 4; ++k) {
      int xx = tmp.x + d[k][0], yy = tmp.y + d[k][1];
      if (vis[xx][yy] || !legal(xx, yy)) continue;
      vis[xx][yy] = 1;
      dis[xx][yy] = dis[tmp.x][tmp.y] + 1;
      q[tail++] = point(xx, yy);
    }
  }
}
int main() {
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 1; i <= h; ++i) {
    scanf("%s", mz[i] + 1);
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (mz[i][j] == 'S') sx = i, sy = j;
    }
  }
  dfs1(sx, sy, 0);
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (vis[i][j]) q[tail++] = point(i, j), dis[i][j] = 0;
    }
  }
  bfs();
  int ans = 0x3f3f3f3f;
  for (int i = 1; i <= h; ++i) {
    ans = min(ans, 1 + (dis[i][1] + k - 1) / k);
    ans = min(ans, 1 + (dis[i][w] + k - 1) / k);
  }
  for (int i = 1; i <= w; ++i) {
    ans = min(ans, 1 + (dis[1][i] + k - 1) / k);
    ans = min(ans, 1 + (dis[h][i] + k - 1) / k);
  }
  printf("%d\n", ans);
  return 0;
}
