#include <bits/stdc++.h>
using namespace std;
int H, W, K;
char A[800][800];
bool used[800][800];
bool isValid(pair<int, int> p) {
  if (p.first >= 0 && p.first < H && p.second >= 0 && p.second < W)
    return true;
  else
    return false;
}
void dfs(int d, int h, int w) {
  if (A[h][w] == '#') return;
  if (used[h][w]) return;
  used[h][w] = true;
  if (d == K) return;
  if (isValid(pair<int, int>(h - 1, w))) dfs(d + 1, h - 1, w);
  if (isValid(pair<int, int>(h + 1, w))) dfs(d + 1, h + 1, w);
  if (isValid(pair<int, int>(h, w - 1))) dfs(d + 1, h, w - 1);
  if (isValid(pair<int, int>(h, w + 1))) dfs(d + 1, h, w + 1);
}
int main() {
  cin >> H >> W >> K;
  pair<int, int> s;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') s = pair<int, int>(i, j);
      used[i][j] = false;
    }
  }
  dfs(0, s.first, s.second);
  int min_dist = 800;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (used[i][j]) {
        min_dist = min(min_dist, min({i, j, H - i - 1, W - j - 1}));
      }
    }
  }
  cout << 1 + ((min_dist + K - 1) / K) << endl;
}
