#include <bits/stdc++.h>
using namespace std;
struct pq {
  int weight;
  pair<int, int> pos;
  pq(int _weight, pair<int, int> _pos) {
    weight = _weight;
    pos = _pos;
  }
  pq() {}
  friend bool operator<(pq a, pq b) { return a.weight > b.weight; }
};
const int tb[] = {0, 1, 0, -1};
const int tk[] = {1, 0, -1, 0};
int baris, kolom, k, jarak[805][805], ans;
char petas[805][805];
priority_queue<pq> cari;
bool masuk(int bar, int kol) {
  return (1 <= bar && bar <= baris && 1 <= kol && kol <= kolom);
}
bool pinggir(int bar, int kol) {
  return (bar == 1 || bar == baris || kol == 1 || kol == kolom);
}
bool masuk(pair<int, int> pos) { return masuk(pos.first, pos.second); }
bool pinggir(pair<int, int> pos) { return pinggir(pos.first, pos.second); }
int main() {
  scanf("%d%d%d", &baris, &kolom, &k);
  for (int i = 1; i <= baris; i++) {
    scanf("\n");
    for (int j = 1; j <= kolom; j++) {
      scanf("%c", &petas[i][j]);
      if (petas[i][j] == 'S') {
        jarak[i][j] = 0;
        cari.push(pq(jarak[i][j], make_pair(i, j)));
        petas[i][j] = '.';
      } else
        jarak[i][j] = baris * kolom + 5;
    }
  }
  while (!cari.empty()) {
    pq depan = cari.top();
    cari.pop();
    pair<int, int> pos = depan.pos;
    if (jarak[pos.first][pos.second] != depan.weight) continue;
    if (pinggir(pos)) {
      ans = (depan.weight + k - 1) / k;
      break;
    }
    for (int ulang = 0; ulang < 4; ulang++) {
      pair<int, int> nextpos = pos;
      nextpos.first += tb[ulang];
      nextpos.second += tk[ulang];
      if (!masuk(nextpos)) continue;
      int biaya;
      biaya = depan.weight + 1;
      if (petas[nextpos.first][nextpos.second] == '#')
        biaya = max(biaya, k + 1);
      if (biaya < jarak[nextpos.first][nextpos.second]) {
        jarak[nextpos.first][nextpos.second] = biaya;
        cari.push(pq(biaya, nextpos));
      }
    }
  }
  cout << ans << endl;
}
