#include <bits/stdc++.h>
using namespace std;
const int MAX = 810;
const int DX[4] = {-1, 0, 1, 0};
const int DY[4] = {0, -1, 0, 1};
int H, W, K, sx, sy;
bool f[MAX][MAX];
long long solve() {
  set<pair<long long, long long> > pos;
  vector<pair<long long, long long> > p;
  pair<long long, long long> sxy(sx, sy);
  pos.insert(sxy);
  p.push_back(sxy);
  for (long long k = 0; k < K; ++k) {
    vector<pair<long long, long long> > p1;
    for (const auto& r : p) {
      for (long long i = 0; i < 4; ++i) {
        int x = r.first + DX[i], y = r.second + DY[i];
        pair<long long, long long> q = pair<long long, long long>(x, y);
        if (!f[y][x] && pos.find(q) == pos.end()) {
          pos.insert(q);
          p1.push_back(q);
          if (x == 0 || y == 0 || x == W - 1 || y == H - 1) return 1;
        }
      }
    }
    swap(p, p1);
  }
  long long d = min(H, W);
  for (auto iter = pos.begin(); iter != pos.end(); iter++) {
    long long x = (*iter).first, y = (*iter).second;
    d = min(d, min({x, y, W - x - 1, H - y - 1}));
  }
  return d / K + 1;
}
int main() {
  cin >> H >> W >> K;
  for (long long h = 0; h < H; ++h) {
    string a;
    cin >> a;
    for (long long w = 0; w < W; ++w) {
      if (a[w] == 'S') {
        sx = w;
        sy = h;
      } else if (a[w] == '#') {
        f[h][w] = true;
      }
    }
  }
  cout << solve() << endl;
}
