#include <bits/stdc++.h>
using namespace std;
int dh[] = {1, -1, 0, 0};
int dw[] = {0, 0, 1, -1};
int main() {
  int h, w;
  int k;
  cin >> h >> w >> k;
  vector<string> a(h);
  for (int i = 0; i < h; i++) cin >> a[i];
  pair<int, int> s;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        s.first = i;
        s.second = j;
        i = h;
        j = w;
      }
    }
  }
  auto isrange = [&h, &w](int i, int j) {
    return 0 <= i && i < h && 0 <= j && j < w;
  };
  queue<pair<pair<int, int>, pair<int, int>>> que;
  vector<vector<vector<int>>> dp(h,
                                 vector<vector<int>>(w, vector<int>(801, -1)));
  que.push(make_pair(s, make_pair(0, 0)));
  while (!que.empty()) {
    auto state = que.front();
    que.pop();
    auto place = state.first;
    auto charge = state.second.first;
    int score = state.second.second;
    if (place.first == 0 || place.first == h - 1 || place.second == 0 ||
        place.second == w - 1) {
      cout << (score / k) + (score % k != 0) << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      int toh = place.first + dh[i];
      int tow = place.second + dw[i];
      if (!isrange(toh, tow)) continue;
      int magic = min(800, charge + k - charge - (a[toh][tow] == '#'));
      if (!(a[toh][tow] == '#' && charge == 0) && dp[toh][tow][magic] == -1) {
        dp[toh][tow][magic] = score + 1;
        que.push(make_pair(make_pair(toh, tow), make_pair(magic, score + 1)));
      }
    }
    if (charge + k <= 800) {
      if (dp[place.first][place.second][charge + k] == -1) {
        dp[place.first][place.second][charge + k] = score + k;
        que.push(make_pair(place, make_pair(charge + k, score + k)));
      }
    } else {
      if (dp[place.first][place.second][800] == -1) {
        dp[place.first][place.second][800] = score + k;
        que.push(make_pair(place, make_pair(800, score + 1)));
      }
    }
  }
  return 0;
}
