#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;
long long minD = INF;
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;
}
void dfs(long long i, long long j, long long k) {
  used[i][j] = true;
  long long mini = min({i, j, H - 1 - i, W - 1 - j});
  minD = min(minD, mini);
  if (--k == 0) return;
  for (int d = 0; d < 4; d++) {
    long long I = i + x[d], J = j + y[d];
    if (in_grid(I, J)) {
      if (!used[I][J] && G[I][J] == '.') dfs(I, J, k);
    }
  }
  return;
}
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) cin >> G[i];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (G[i][j] == 'S') dfs(i, j, K);
  cout << 1 + (minD + K - 1) / K << endl;
}
