#include <bits/stdc++.h>
using namespace std;
int GLL(long long& x) { return scanf("%lld", &x); }
int GI(int& x) { return scanf("%d", &x); }
int h, w, k, sh, sw;
int g[810][810];
int moves[810][810];
int r_idx[] = {-1, 1, 0, 0};
int c_idx[] = {0, 0, -1, 1};
priority_queue<pair<int, pair<int, int> > > Q;
void unlock(int dist, int r, int c, int cnt) {
  g[r][c] = 1;
  if (cnt == k) return;
  for (int i = 0; i < (int)(4); i++) {
    int nr = r + r_idx[i];
    int nc = c + c_idx[i];
    if (nr < 1 || nr > h || nc < 1 || nc > w) continue;
    if (moves[nr][nc] >= 0) continue;
    if (g[nr][nc] != 0) continue;
    if (cnt == k)
      continue;
    else {
      unlock(dist, nr, nc, cnt + 1);
    }
  }
}
void step(int dist, int r, int c, int cnt) {
  moves[r][c] = dist;
  for (int i = 0; i < (int)(4); i++) {
    int nr = r + r_idx[i];
    int nc = c + c_idx[i];
    if (nr < 1 || nr > h || nc < 1 || nc > w) continue;
    if (moves[nr][nc] >= 0) continue;
    if (g[nr][nc] == 0 || cnt == k)
      ;
    else {
      Q.push(make_pair(-dist, make_pair(nr, nc)));
      step(dist, nr, nc, cnt + 1);
    }
  }
  for (int i = 0; i < (int)(4); i++) {
    int nr = r + r_idx[i];
    int nc = c + c_idx[i];
    if (nr < 1 || nr > h || nc < 1 || nc > w) continue;
    if (moves[nr][nc] >= 0) continue;
    if (g[nr][nc] == 0 || cnt == k) unlock(dist, nr, nc, 1);
  }
}
int main() {
  GI(h);
  GI(w);
  GI(k);
  memset(moves, -1, sizeof moves);
  for (int i = 1; i <= (int)(h); i++) {
    string line;
    cin >> line;
    for (int j = 0; j < (int)(w); j++) {
      if (line[j] == '#')
        g[i][j + 1] = 0;
      else
        g[i][j + 1] = 1;
      if (line[j] == 'S') {
        sh = i;
        sw = j + 1;
      }
    }
  }
  Q.push(make_pair(0, make_pair(sh, sw)));
  while (Q.size() > 0) {
    pair<int, pair<int, int> > node = Q.top();
    Q.pop();
    int dist = -node.first;
    int r = node.second.first;
    int c = node.second.second;
    moves[r][c] = dist;
    if (r == h || r == 1 || c == w || c == 1) {
      printf("%d\n", dist);
      return 0;
    }
    step(dist + 1, r, c, 0);
    if (Q.size() == 0) {
      Q.push(make_pair(-dist - 1, make_pair(r, c)));
    }
  }
  return 0;
}
