#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char a[810][810], tmp[810][810];
int dd[] = {1, 0, -1, 0, 1};
int si, sj;
int dist[810][810];
bool valid(int i, int j) { return i >= 0 && i < h && j >= 0 && j < w; }
bool end(int i, int j) { return i == 0 || i == h - 1 || j == 0 || j == w - 1; }
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 < dist[i][0] ? mind : dist[i][0]);
    mind = (mind < dist[i][w - 1] ? mind : dist[i][w - 1]);
  }
  for (long long i = (long long)(0); i < (long long)(w); i++) {
    mind = (mind < dist[0][i] ? mind : dist[0][i]);
    mind = (mind < dist[h - 1][i] ? mind : dist[h - 1][i]);
  }
  if (mind <= k) {
    cout << 1 << endl;
    return 0;
  }
  int res = INT_MAX / 3;
  memcpy(tmp, a, sizeof(a));
  for (long long d = (long long)(0); d < (long long)(4); d++) {
    int sum = 0;
    int pi = si, pj = sj;
    while (1) {
      sum++;
      for (int cnt = 0; cnt < k;) {
        if (valid(pi + dd[d], pj + dd[d + 1]) &&
            a[pi + dd[d]][pj + dd[d + 1]] == '#') {
          break;
        }
        pi += dd[d];
        pj += dd[d + 1];
        cnt++;
        if (end(pi, pj)) break;
      }
      if (end(pi, pj)) break;
      for (int ni = si, nj = sj, cnt = 0; cnt < k;
           ni += dd[d], nj += dd[d + 1]) {
        if (!valid(ni, nj)) break;
        if (a[ni][nj] == '#') {
          a[ni][nj] = '.';
          cnt++;
        }
      }
    }
    memcpy(a, tmp, sizeof(tmp));
    res = (res < sum ? res : sum);
  }
  cout << res << endl;
  return 0;
}
