#include <bits/stdc++.h>
using namespace std;
int H, W, sx, sy, ans, k;
char s[802][802];
int dis[802][802];
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
void check(int x, int y) {
  ans = min(ans, 1 + min((x + k - 1) / k, (H - x + k - 1) / k));
  ans = min(ans, 1 + min((y + k - 1) / k, (W - y + k - 1) / k));
}
void bfs(int x, int y, int z) {
  if (z > k) return;
  check(x, y);
  dis[x][y] = z;
  int xx, yy;
  for (int i = 0; i < 4; ++i) {
    xx = x + dx[i];
    yy = y + dy[i];
    if (xx >= 1 && xx <= H && yy >= 1 && yy <= W)
      if (dis[xx][yy] == 1e9 && s[xx][yy] != '#') bfs(xx, yy, z + 1);
  }
}
int main() {
  scanf("%d %d %d", &H, &W, &k);
  for (int i = 1; i <= H; ++i) scanf("%s", s[i] + 1);
  for (int i = 1; i <= H; ++i)
    for (int j = 1; j <= W; ++j)
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
        break;
      }
  for (int i = 1; i <= H; ++i)
    for (int j = 1; j <= W; ++j) dis[i][j] = 1e9;
  ans = 1e9;
  bfs(sx, sy, 0);
  cout << ans;
  return 0;
}
