#include <bits/stdc++.h>
using namespace std;
bool isblocked[800][800];
int dist[800][800];
int H, W, K;
int main() {
  int sx, sy;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < W; j++) {
      if (str[j] == 'S') {
        sx = j;
        sy = i;
        str[j] = '.';
      }
      isblocked[i][j] = (str[j] == '#');
    }
  }
  fill(dist[0], dist[800], 10000000);
  dist[sy][sx] = 0;
  deque<pair<int, int> > que;
  que.emplace_back(sx, sy);
  while (!que.empty()) {
    int x, y;
    tie(x, y) = que.front();
    que.pop_front();
    int k = dist[y][x];
    if (k >= K) continue;
    if (x > 0 && !isblocked[y][x - 1] && dist[y][x - 1] > k + 1) {
      dist[y][x - 1] = k + 1;
      que.emplace_back(x - 1, y);
    }
    if (x + 1 < W && !isblocked[y][x + 1] && dist[y][x + 1] > k + 1) {
      dist[y][x + 1] = k + 1;
      que.emplace_back(x + 1, y);
    }
    if (y > 0 && !isblocked[y - 1][x] && dist[y - 1][x] > k + 1) {
      dist[y - 1][x] = k + 1;
      que.emplace_back(x, y - 1);
    }
    if (y + 1 < H && !isblocked[y + 1][x] && dist[y + 1][x] > k + 1) {
      dist[y + 1][x] = k + 1;
      que.emplace_back(x, y + 1);
    }
  }
  int ans = 10000000;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (dist[j][i] <= K) {
        int len = min({i, j, H - 1 - i, W - 1 - j});
        int t = 1 + (len + (K - 1)) / K;
        ans = min(ans, t);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
