#include <bits/stdc++.h>
using namespace std;
long long h, w, k, memo[800][800], ans = LONG_LONG_MAX;
queue<pair<long long, long long>> que;
bool visited[800][800];
pair<long long, long long> st;
string a[800];
bool is_unsafe(long long y, long long x) {
  return x < 0 || y < 0 || x >= w || y >= h;
}
signed main() {
  cin >> h >> w >> k;
  for (long long i = 0; i < (long long)(h); ++i) cin >> a[i];
  for (long long i = 0; i < (long long)(h); ++i)
    for (long long j = 0; j < (long long)(w); ++j)
      memo[i][j] = min(min(i + 1, j + 1), min(h - i, w - j)) / k;
  for (long long i = 1; i < (long long)(h - 1); ++i)
    for (long long j = 1; j < (long long)(w - 1); ++j)
      memo[i][j] = max(memo[i][j], 1LL);
  for (long long i = 0; i < (long long)(h); ++i)
    for (long long j = 0; j < (long long)(w); ++j)
      if (a[i][j] == 'S') que.push({i, j});
  for (long long hoge = 0; hoge < (long long)(k + 1); ++hoge) {
    long long dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
    queue<pair<long long, long long>> tmpque;
    while (!que.empty()) {
      long long y = que.front().first, x = que.front().second;
      que.pop();
      if (visited[y][x]) continue;
      visited[y][x] = true;
      ans = min(ans, memo[y][x]);
      for (long long i = 0; i < (long long)(4); ++i) {
        long long Y = y + dy[i], X = x + dx[i];
        if (is_unsafe(Y, X)) continue;
        if (a[Y][X] == '#') continue;
        tmpque.push({Y, X});
      }
    }
    que = tmpque;
  }
  cout << ans + 1 << endl;
}
