#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char a[810][810];
int dd[] = {1, 0, -1, 0, 1};
int si, sj;
int dist[810][810], dist2[810][810];
bool valid(int i, int j) { return i >= 0 && i < h && j >= 0 && j < w; }
int manh(int i, int j) { return abs(i - si) + abs(j - sj); }
int main() {
  cin.sync_with_stdio(false);
  cin >> h >> w >> k;
  for (long long i = (long long)(0); i < (long long)(h); i++) {
    cin >> a[i];
    for (long long j = (long long)(0); j < (long long)(w); j++)
      if (a[i][j] == 'S') {
        si = i;
        sj = j;
        a[i][j] = '.';
      }
  }
  for (long long i = (long long)(0); i < (long long)(h); i++)
    for (long long j = (long long)(0); j < (long long)(w); j++)
      dist[i][j] = INT_MAX / 3;
  queue<pair<pair<long long, long long>, long long> > que;
  que.push(pair<pair<long long, long long>, long long>(
      pair<long long, long long>(si, sj), 0));
  while (que.size()) {
    pair<pair<long long, long long>, long long> np = que.front();
    que.pop();
    int ni = np.first.first, nj = np.first.second;
    if (dist[ni][nj] != INT_MAX / 3) continue;
    dist[ni][nj] = np.second;
    for (long long d = (long long)(0); d < (long long)(4); d++) {
      int pi = ni + dd[d], pj = nj + dd[d + 1];
      if (valid(pi, pj) && a[pi][pj] != '#')
        que.push(pair<pair<long long, long long>, long long>(
            pair<long long, long long>(pi, pj), np.second + 1));
    }
  }
  int mind = INT_MAX / 3;
  for (long long i = (long long)(0); i < (long long)(h); i++) {
    mind = (mind < min(dist[i][0], manh(i, 0) + k)
                ? mind
                : min(dist[i][0], manh(i, 0) + k));
    mind = (mind < min(dist[i][w - 1], manh(i, w - 1) + k)
                ? mind
                : min(dist[i][w - 1], manh(i, w - 1) + k));
  }
  for (long long i = (long long)(0); i < (long long)(w); i++) {
    mind = (mind < min(dist[0][i], manh(0, i) + k)
                ? mind
                : min(dist[0][i], manh(0, i) + k));
    mind = (mind < min(dist[h - 1][i], manh(h - 1, i) + k)
                ? mind
                : min(dist[h - 1][i], manh(h - 1, i) + k));
  }
  cout << ceil((double)mind / k) << endl;
  return 0;
}
