#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
const int INF = 1 << 29;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int dx[] = {0, 1, 0, -1, 0}, dy[] = {0, 0, -1, 0, 1};
int H, W, K;
const int MAX_HW = 810;
const int MAX_K = MAX_HW * MAX_HW;
char board[MAX_HW][MAX_HW];
int visited[MAX_HW][MAX_HW][MAX_HW / 2];
int sy, sx;
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> board[i][j];
      if (board[i][j] == 'S') {
        sy = i;
        sx = j;
      }
    }
  }
  queue<pair<pii, pii>> que;
  bool ok = false;
  for (int i = 1; i < 5; i++) {
    int ny = sy + dy[i];
    int nx = sx + dx[i];
    if (board[ny][nx] != '#') {
      ok = true;
    }
  }
  if (ok) {
    que.emplace(make_pair(make_pair(sy, sx), make_pair(0, 0)));
  } else {
    que.emplace(make_pair(make_pair(sy, sx), make_pair(K, K)));
  }
  int result = 1 << 30;
  while (not que.empty()) {
    auto p = que.front();
    que.pop();
    auto pos = p.first;
    auto state = p.second;
    int py, px;
    py = pos.first;
    px = pos.second;
    int cost, rest;
    cost = state.first;
    rest = state.second;
    if (result < cost) continue;
    if (visited[py][px][rest]) continue;
    visited[py][px][rest] = true;
    if (py == 0 or py == H - 1 or px == 0 or px == W - 1) {
      result = cost;
      break;
    }
    int ny, nx;
    for (int i = 1; i < 5; i++) {
      ny = py + dy[i];
      nx = px + dx[i];
      int n_rest = rest;
      int n_cost = cost + 1;
      if (board[ny][nx] == '#') {
        n_rest--;
      }
      if (n_rest < 0) continue;
      if (n_cost > 0 and n_cost % K == 0) {
        n_rest = n_rest + K;
        n_rest = min(n_rest, MAX_HW / 2);
      }
      if (visited[ny][nx][rest]) continue;
      que.emplace(make_pair(make_pair(ny, nx), make_pair(n_cost, n_rest)));
    }
  }
  cout << (result + K - 1) / K << endl;
  return 0;
}
