#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
using namespace std;
const long long mod = 1000000007;
long long h, w, k;
char table[810][810];
long long dist[810][810];
long long si, sj;
long long ml, mr, mu, md;
long long dxy[5] = {0, 1, 0, -1, 0};
signed main() {
  scanf("%lld", &(h));
  scanf("%lld", &(w));
  scanf("%lld", &(k));
  for (long long i = 0; i < (long long)(h); i++) {
    for (long long j = 0; j < (long long)(w); j++) {
      table[i + 1][j + 1] = getchar();
      if (table[i + 1][j + 1] == 'S') {
        si = i + 1;
        sj = j + 1;
      }
    }
    getchar();
  }
  for (long long i = 0; i < (long long)(810); i++)
    for (long long j = 0; j < (long long)(810); j++) dist[i][j] = 1145141919LL;
  dist[si][sj] = 0;
  deque<pair<long long, long long> > q;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    auto p = q.front();
    long long d = dist[p.first][p.second];
    q.pop_front();
    for (long long i = 0; i < (long long)(4); i++)
      if (table[p.first + dxy[i]][p.second + dxy[i + 1]] == '.') {
        dist[p.first + dxy[i]][p.second + dxy[i + 1]] = d + 1;
        if (d + 1 <= k) {
          q.emplace_back(p.first + dxy[i], p.second + dxy[i + 1]);
        }
        table[p.first + dxy[i]][p.second + dxy[i + 1]] = 'S';
      }
  }
  ml = 1000;
  mr = -1;
  mu = 1000;
  md = -1;
  for (long long i = 0; i < (long long)(h); i++)
    for (long long j = 0; j < (long long)(w); j++) {
      if (dist[i + 1][j + 1] <= k) {
        ml = min(ml, j);
        mr = max(mr, j);
        mu = min(mu, i);
        md = max(md, i);
      }
    }
  long long ans = 1145141919LL;
  ans = min(ans, (ml + k - 1) / k);
  ans = min(ans, (mu + k - 1) / k);
  ans = min(ans, ((w - 1 - mr) + k - 1) / k);
  ans = min(ans, ((h - 1 - md) + k - 1) / k);
  cout << ans + 1 << endl;
}
