#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long long int INF = (long long int)1e15;
string S[1000];
struct Node {
  long long int x;
  long long int y;
  long long int cost;
  Node(long long int x, long long int y, long long int c)
      : x(x), y(y), cost(c) {}
};
long long int H, W, K;
long long int x[4] = {1, 0, -1, 0};
long long int y[4] = {0, 1, 0, -1};
long long int M[805][805];
long long int length(long long int h, long long int w) {
  return std::min(std::min(h, H - 1 - h), std::min(w, W - 1 - w));
}
int main() {
  cin >> H >> W >> K;
  for (long long int(i) = (0); (i) < (H); ++(i)) {
    cin >> S[i];
  }
  pair<long long int, long long int> s;
  for (long long int(i) = (0); (i) < (H); ++(i)) {
    for (long long int(j) = (0); (j) < (W); ++(j)) {
      if (S[i][j] == 'S') s = pair<long long int, long long int>(i, j);
      M[i][j] = INF;
    }
  }
  long long int min = INF;
  queue<Node> q;
  q.push(Node(s.first, s.second, 0));
  while (!q.empty()) {
    auto t = q.front();
    q.pop();
    if (M[t.x][t.y] < t.cost) continue;
    M[t.x][t.y] = t.cost;
    min = std::min(min, length(t.x, t.y));
    if (t.cost >= K) continue;
    for (long long int(i) = (0); (i) < (4); ++(i)) {
      long long int nx = t.x + x[i];
      long long int ny = t.y + y[i];
      long long int cost = t.cost + 1;
      if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
      if (M[nx][ny] <= cost || S[nx][ny] == '#') continue;
      q.push(Node(nx, ny, cost));
    }
  }
  cout << (min + K - 1) / K + 1 << endl;
  return 0;
}
