#include <bits/stdc++.h>
using namespace std;
long long dx[4] = {1, 0, -1, 0};
long long dy[4] = {0, -1, 0, 1};
long long nextInt() {
  long long a;
  cin >> a;
  return a;
}
char nextChar() {
  char a;
  cin >> a;
  return a;
}
double nextDouble() {
  double a;
  cin >> a;
  return a;
}
string nextString() {
  string a;
  cin >> a;
  return a;
}
template <class T>
void inputVector(vector<T>& v, long long n) {
  v.resize(n);
  for (long long i = 0; i < v.size(); i++) cin >> v[i];
}
long long H, W, K;
long long sx, sy;
vector<string> board;
bool inBoard(long long x, long long y) {
  return x >= 0 && x < W && y >= 0 && y < H;
}
bool isEdge(long long x, long long y) {
  return x == 0 || x == W - 1 || y == 0 || y == H - 1;
}
class Info {
 public:
  long long x, y;
  long long used;
  Info() {}
  Info(long long x, long long y, long long used) {
    this->x = x;
    this->y = y;
    this->used = used;
  }
};
long long memo[810][810];
signed main() {
  cin >> H >> W >> K;
  inputVector(board, H);
  for (long long y = 0; y < (H); y++)
    for (long long x = 0; x < (W); x++) {
      if (board[y][x] == 'S') {
        sx = x;
        sy = y;
        break;
      }
    }
  for (long long y = 0; y < (H); y++)
    for (long long x = 0; x < (W); x++) {
      memo[y][x] = -1;
    }
  bool flag = true;
  long long cnt = 0;
  queue<Info> q;
  q.push(Info(sx, sy, 0));
  while (flag) {
    cnt++;
    queue<Info> nq;
    while (!q.empty()) {
      Info nowinfo = q.front();
      q.pop();
      if (isEdge(nowinfo.x, nowinfo.y)) {
        cout << cnt - 1 << endl;
        flag = false;
        break;
      }
      queue<Info> tmpq;
      tmpq.push(nowinfo);
      long long left = K;
      while (left-- && !tmpq.empty()) {
        queue<Info> ntmpq;
        while (!tmpq.empty()) {
          Info nowinfo2 = tmpq.front();
          tmpq.pop();
          for (long long dir = 0; dir < (4); dir++) {
            long long nx = nowinfo2.x + dx[dir];
            long long ny = nowinfo2.y + dy[dir];
            if (!inBoard(nx, ny)) continue;
            long long used = nowinfo2.used + (board[ny][nx] == '#');
            if (used > (cnt - 1) * K) continue;
            if (used <= memo[ny][nx]) continue;
            memo[ny][nx] = used;
            ntmpq.push(Info(nx, ny, used));
            nq.push(Info(nx, ny, used));
          }
        }
        tmpq = ntmpq;
      }
    }
    if (cnt == 1) {
      nq.push(Info(sx, sy, 0));
    }
    q = nq;
  }
  return 0;
}
