#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  string field[820];
  int sh, sw;
  for (int i = 1; i <= H; i++) {
    cin >> field[i];
    field[i] = "." + field[i];
    for (int j = 1; j <= W; j++) {
      if (field[i][j] == 'S') {
        sh = i;
        sw = j;
      }
    }
  }
  int map1[820][820];
  int map2[820][820];
  for (int i = 0; i <= 800; i++) {
    for (int j = 0; j <= 800; j++) {
      map1[i][j] = (1e9);
      map2[i][j] = (1e9);
    }
  }
  map1[sh][sw] = 0;
  int dh[4] = {1, -1, 0, 0};
  int dw[4] = {0, 0, 1, -1};
  queue<pair<int, int> > que;
  que.push({sh, sw});
  while (!que.empty()) {
    pair<int, int> out = que.front();
    que.pop();
    int h = out.first;
    int w = out.second;
    if ((h == 0) || (h == H) || (w == 0) || (w == W)) {
      cout << 1 << endl;
      return 0;
    }
    if (map1[h][w] >= K) continue;
    for (int k = 0; k < 4; k++) {
      int newh = h + dh[k];
      int neww = w + dw[k];
      if ((neww <= 0) || (neww > W)) continue;
      if ((newh <= 0) || (newh > H)) continue;
      if (field[newh][neww] == '#') continue;
      if (map1[newh][neww] <= map1[h][w] + 1) continue;
      map1[newh][neww] = map1[h][w] + 1;
      que.push({newh, neww});
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (map1[i][j] <= K) {
        que.push({i, j});
        map2[i][j] = K;
      }
    }
  }
  while (!que.empty()) {
    pair<int, int> out = que.front();
    que.pop();
    int h = out.first;
    int w = out.second;
    if ((h == 0) || (h == H) || (w == 0) || (w == W)) {
      cout << (map2[h][w] + K - 1) / K << endl;
      return 0;
    }
    for (int k = 0; k < 4; k++) {
      int newh = h + dh[k];
      int neww = w + dw[k];
      if ((neww <= 0) || (neww > W)) continue;
      if ((newh <= 0) || (newh > H)) continue;
      if (map2[newh][neww] <= map2[h][w] + 1) continue;
      map2[newh][neww] = map2[h][w] + 1;
      que.push({newh, neww});
    }
  }
  return 0;
}
