#include <bits/stdc++.h>
using namespace std;
int H, W, K;
char map[808][808];
int dist[808][808];
int main() {
  scanf("%d %d %d", &H, &W, &K);
  queue<pair<int, int> > Q;
  for (int i = 0; i < H; i++) {
    scanf("%s", map[i]);
    for (int j = 0; j < W; j++) {
      dist[i][j] = -1;
      if (map[i][j] == 'S') {
        Q.push({i, j});
        dist[i][j] = 0;
      }
    }
  }
  while (!Q.empty()) {
    int x = Q.front().first, y = Q.front().second;
    Q.pop();
    int dx[4] = {0, 1, 0, -1};
    int dy[4] = {1, 0, -1, 0};
    for (int k = 0; k < 4; k++) {
      int px = x + dx[k], py = y + dy[k];
      if (px < 0 || px >= H || py < 0 || py >= W) continue;
      if (dist[px][py] == -1 && map[px][py] != '#') {
        Q.push({px, py});
        dist[px][py] = dist[x][y] + 1;
      }
    }
  }
  int ans = 0x7fffffff;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (dist[i][j] != -1 && dist[i][j] <= K) {
        int r = (min({i, H - i - 1, j, W - j - 1}) + K - 1) / K + 1;
        ans = min(r, ans);
      }
  printf("%d\n", ans);
  return 0;
}
