#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);
  }
  queue<int> q;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dist[i][j] = inf;
      if (dat[i][j] == 'S') {
        dat[i][j] = '.';
        dist[i][j] = 0;
        q.push(1000 * i + j);
      }
    }
  }
  while (q.size()) {
    int v = q.front();
    q.pop();
    int i = v / 1000;
    int j = v % 1000;
    int di[4] = {-1, 0, 1, 0};
    int dj[4] = {0, 1, 0, -1};
    for (int k = 0; k < 4; k++) {
      int i_ = i + di[k];
      int j_ = j + dj[k];
      if (0 <= i_ && i_ < H && 0 <= j_ && j_ < W && dat[i_][j_] == '.') {
        if (dist[i][j] + 1 >= dist[i_][j_]) continue;
        dist[i_][j_] = dist[i][j] + 1;
        q.push(1000 * i_ + j_);
      }
    }
  }
  ll ans = inf;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (dist[i][j] <= K) {
        int dist1 = i;
        int dist2 = H - 1 - i;
        int dist3 = j;
        int dist4 = W - 1 - j;
        if (!dist1 || !dist2 || !dist3 || !dist4) {
          cout << 1 << endl;
          return 0;
        }
        int temp = 2 + (min(min(dist1, dist2), min(dist3, dist4) - 1) / K);
        ans = min(ans, (ll)temp);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
