#include <bits/stdc++.h>
const bool debug = false;
using namespace std;
const long long MOD = 1000000007;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  int h0, w0;
  vector<string> s(h);
  for (int(i) = 0; (i) < (h); (i)++) {
    cin >> s[i];
  }
  for (int(i) = 0; (i) < (h); (i)++) {
    for (int(j) = 0; (j) < (w); (j)++) {
      if (s[i][j] == 'S') {
        h0 = i;
        w0 = j;
      }
    }
  }
  queue<pair<int, int>> que;
  vector<vector<bool>> check(h, vector<bool>(w, false));
  int d = h0;
  chmin(d, w0);
  chmin(d, w - w0);
  chmin(d, h - h0);
  que.emplace(h0, w0);
  while (!que.empty()) {
    auto now = que.front();
    int x = now.first, y = now.second;
    if (debug == true) {
      cout << x << " " << y << " " << endl;
    }
    que.pop();
    for (int i = -1; i <= 1; i += 2) {
      if (x + i >= 0 && x + i < h && s[x + i][y] == '.' && !check[x + i][y] &&
          abs(h0 - x - i) + abs(w0 - y) <= k) {
        check[x + i][y] = true;
        que.emplace(x + i, y);
        chmin(d, x + i);
        chmin(d, y);
        chmin(d, h - x - i);
        chmin(d, w - y);
      }
      if (y + i >= 0 && y + i < w && s[x][y + i] == '.' && !check[x][y + i] &&
          abs(h0 - x) + abs(w0 - y - i) <= k) {
        check[x][y + i] = true;
        que.emplace(x, y + i);
        chmin(d, x);
        chmin(d, y + i);
        chmin(d, h - x);
        chmin(d, w - y - i);
      }
    }
  }
  if (d == 0) {
    cout << 1 << endl;
    return 0;
  }
  long long res = 1;
  res += (d - 1) / k + 1;
  cout << res << endl;
  return 0;
}
