#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 805;
const int dx[4] = {1, 0, 0, -1};
const int dy[4] = {0, 1, -1, 0};
bool visit[MAX_N][MAX_N];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, K;
  cin >> h >> w >> K;
  vector<string> s(h);
  int sx, sy;
  for (int i = 0; i < (int)(h); ++i) {
    cin >> s[i];
    for (int j = 0; j < (int)(w); ++j) {
      if (s[i][j] == 'S') {
        sx = i, sy = j;
      }
    }
  }
  queue<pair<int, int> > que;
  visit[sx][sy] = true;
  que.push(pair<int, int>(sx, sy));
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    for (int i = 0; i < (int)(4); ++i) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (nx >= 0 && nx < h && ny >= 0 && ny < w && !visit[nx][ny] &&
          s[nx][ny] != '#') {
        visit[nx][ny] = true;
        que.push(pair<int, int>(nx, ny));
      }
    }
  }
  int dist = 1000000005;
  for (int i = 0; i < (int)(h); ++i) {
    for (int j = 0; j < (int)(w); ++j) {
      if (visit[i][j]) dist = min(dist, min({i, h - 1 - i, j, w - 1 - j}));
    }
  }
  cout << (dist + K - 1) / K + 1 << "\n";
  return 0;
}
