#include <bits/stdc++.h>
using namespace std;
int H, W, K;
char A[880][880];
int ans;
bool used[880][880];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
void dfs(int x, int y, int cost) {
  used[x][y] = true;
  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 (cost == K) return;
  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;
    dfs(nx, ny, cost + 1);
  }
  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;
  dfs(start.first, start.second, 0);
  cout << ans + 1 << endl;
  return 0;
}
