#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y, dis;
};
node Q[802 * 802];
bool vis[802][802];
int H, W, sx, sy, ans, k;
char s[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 l = 0, r = 1;
  Q[1].x = sx;
  Q[1].y = sy;
  Q[1].dis = 0;
  vis[sx][sy] = 1;
  check(sx, sy);
  while (r - l >= 1) {
    int x = Q[++l].x, y = Q[l].y, xx, yy;
    if (Q[l].dis <= k) check(x, y);
    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 (!vis[xx][yy] && s[xx][yy] != '#') {
          vis[xx][yy] = 1;
          Q[++r] = (node){xx, yy, Q[l].dis + 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;
      }
  ans = 1e9;
  memset(vis, 0, sizeof(vis));
  bfs();
  cout << ans;
  return 0;
}
