#include <bits/stdc++.h>
using namespace std;
long long N, M, H, W, K, Q, A, B, L, R;
string S, T;
const long long MOD = 998244353;
const long long INF = 1LL << 60;
vector<string> G(800);
vector<vector<bool> > used(800, vector<bool>(800, false));
vector<long long> x = {-1, 0, 1, 0}, y = {0, -1, 0, 1};
bool in_grid(long long i, long long j) {
  return i >= 0 && i < H && j >= 0 && j < W;
}
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) cin >> G[i];
  long long start, minD = INF;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (G[i][j] == 'S') start = i * H + j;
  queue<pair<long long, long long> > que;
  que.emplace(start, K);
  while (!que.empty()) {
    pair<long long, long long> p = que.front();
    que.pop();
    long long i = p.first / H, j = p.first % H;
    used[i][j] = true;
    minD = min({minD, i, j, H - 1 - i, W - 1 - j});
    if (p.second-- == 0) continue;
    for (int k = 0; k < 4; k++) {
      long long I = i + x[k], J = j + y[k];
      if (in_grid(I, J)) {
        if (!used[I][J] && G[I][J] == '.') que.emplace(I * H + J, p.second);
      }
    }
  }
  cout << 1 + (minD + K - 1) / K << endl;
}
