#include <bits/stdc++.h>
using namespace std;
const int MAX_H = 805;
const int MAX_W = 805;
int H, W, K;
bool visited[MAX_H][MAX_W];
queue<pair<int, pair<int, int>>> q;
int dh[4] = {-1, 0, 1, 0};
int dw[4] = {0, 1, 0, -1};
int main() {
  string s;
  int ans;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    cin >> s;
    for (int j = 0; j < W; j++) {
      visited[i][j] = (s[j] == '#');
      if (s[j] == 'S') {
        q.push(make_pair(0, pair<int, int>(i, j)));
      }
    }
  }
  ans = (max(H, W) + K - 1) / K + 1;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int d = p.first;
    int h = p.second.first;
    int w = p.second.second;
    if (h < 0 || w < 0 || h >= H || w >= W || visited[h][w] || d > K) {
      continue;
    }
    visited[h][w] = true;
    int dist = min(min(h, w), min(H - 1 - h, W - 1 - w));
    ans = min(ans, (dist + K - 1) / K) + 1;
    for (int k = 0; k < 4; k++) {
      q.push(make_pair(d + 1, pair<int, int>(h + dh[k], w + dw[k])));
    }
  }
  cout << ans << endl;
  return 0;
}
