#include <bits/stdc++.h>
using namespace std;
long long h, w, k, sy, sx;
vector<string> s(810);
long long dy[4] = {1, -1, 0, 0};
long long dx[4] = {0, 0, 1, -1};
long long d[810][810];
long long dist[810][810];
const long long INF = 1e9;
bool contain(long long y, long long x) {
  return (0 <= y && y < h && 0 <= x && x < w);
}
signed main() {
  cin >> h >> w >> k;
  for (long long i = 0; i < h; i++) cin >> s[i];
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        sy = i;
        sx = j;
        break;
      }
    }
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      long long tmp = min(i, min(j, min(h - i, w - j)));
      d[i][j] = tmp;
    }
  }
  long long MIN = INF;
  queue<pair<long long, long long> > q;
  q.push(pair<long long, long long>(sy, sx));
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) dist[i][j] = INF;
  dist[sy][sx] = 0;
  while (!q.empty()) {
    pair<long long, long long> p = q.front();
    q.pop();
    long long y = p.first;
    long long x = p.second;
    for (long long i = 0; i < 4; i++) {
      long long ny = y + dy[i];
      long long nx = x + dx[i];
      if (!contain(ny, nx) || dist[ny][nx] != INF) continue;
      dist[ny][nx] = dist[y][x] + 1;
      if (dist[ny][nx] > k) continue;
      MIN = min(MIN, (dist[ny][nx] + k - 1) / k + 1);
      if (d[ny][nx] == 0 && s[ny][nx] == '.') MIN = min(MIN, (long long)1);
      if (s[ny][nx] == '#') continue;
      q.push(pair<long long, long long>(ny, nx));
    }
  }
  cout << MIN << endl;
}
