#include <bits/stdc++.h>
using namespace std;
using Complex = complex<double>;
using ll = long long;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll inf = 1145141919810364364LL;
double PI = acos(-1.0);
vector<string> dat;
ll dist[880][880];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int H, W, K;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    string S;
    cin >> S;
    dat.push_back(S);
  }
  int s, t;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (dat[i][j] == 'S') {
        s = i;
        t = j;
      }
    }
  }
  ll ans = inf;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (abs(i - s) + abs(j - t) <= K) {
        int dist1 = i;
        int dist2 = H - 1 - i;
        int dist3 = j;
        int dist4 = W - 1 - j;
        if (dat[i][j] == '.') {
          if (!dist1 || !dist2 || !dist3 || !dist4) {
            ans = min(ans, 1LL);
            continue;
          }
          int temp = 2 + (min(min(dist1, dist2), min(dist3, dist4)) - 1) / K;
          ans = min(ans, (ll)temp);
        } else {
          if (!dist1 || !dist2 || !dist3 || !dist4) {
            ans = min(ans, 2LL);
            continue;
          }
          int temp = 3 + (min(min(dist1, dist2), min(dist3, dist4)) - 1) / K;
          ans = min(ans, (ll)temp);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
