#include <bits/stdc++.h>
using namespace std;
int H, W, K;
char A[880][880];
int ans;
bool used[880][880];
int dist[880][880];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
void bfs(pair<int, int> start) {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dist[i][j] = 1000000007;
    }
  }
  dist[start.first][start.second] = 0;
  used[start.first][start.second] = true;
  queue<pair<int, int>> Q;
  Q.push(start);
  while (Q.size()) {
    pair<int, int> now = Q.front();
    Q.pop();
    int x = now.first;
    int y = now.second;
    ans = min(ans, (x + K - 1) / K);
    ans = min(ans, (y + K - 1) / K);
    ans = min(ans, (H - 1 - x + K - 1) / K);
    ans = min(ans, (W - 1 - y + K - 1) / K);
    if (dist[x][y] == K) continue;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= H || ny >= W) continue;
      if (used[nx][ny]) continue;
      if (A[nx][ny] == '#') continue;
      dist[nx][ny] = dist[x][y] + 1;
      Q.push(make_pair(nx, ny));
    }
  }
  return;
}
int main() {
  cin >> H >> W >> K;
  pair<int, int> start;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') start = make_pair(i, j);
    }
  }
  ans = 100000;
  bfs(start);
  cout << ans + 1 << endl;
  return 0;
}
