#include <bits/stdc++.h>
using namespace std;
long long int H, W, K;
string MAP[(800)];
int DIST[(800)][(800)];
bool is_inside(int i, int j) { return 0 <= i && i < H && 0 <= j && j < W; }
void solve() {
  int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  int r0, c0;
  queue<pair<int, int> > q1, q2;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      DIST[i][j] = (1000000000 + 7);
      if (MAP[i][j] == 'S') {
        r0 = i;
        c0 = j;
        q1.push(pair<int, int>(i, j));
        q2.push(pair<int, int>(i, j));
        DIST[i][j] = 1;
      }
    }
  }
  while (!q1.empty()) {
    pair<int, int> p = q1.front();
    q1.pop();
    int r = p.first, c = p.second;
    for (int i = 0; i < 4; i++) {
      int rx = r + dir[i][0], cx = c + dir[i][1];
      if (is_inside(rx, cx)) {
        if (MAP[rx][cx] == '.' && DIST[rx][cx] > 1) {
          DIST[rx][cx] = 1;
          q1.push(pair<int, int>(rx, cx));
          q2.push(pair<int, int>(rx, cx));
        }
      }
    }
  }
  long long int ans = -1;
  while (!q2.empty()) {
    pair<int, int> p = q2.front();
    q2.pop();
    int r = p.first, c = p.second;
    if (r == 0 || r == H - 1 || c == 0 || c == W - 1) {
      ans = (DIST[r][c] + K - 2) / K + 1;
      break;
    }
    for (int i = 0; i < 4; i++) {
      int rx = r + dir[i][0], cx = c + dir[i][1];
      if (is_inside(rx, cx)) {
        if (DIST[r][c] + 1 < DIST[rx][cx]) {
          DIST[rx][cx] = DIST[r][c] + 1;
          q2.push(pair<int, int>(rx, cx));
        }
      }
    }
  }
  cout << ans << endl;
}
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) cin >> MAP[i];
  solve();
  return 0;
}
