#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long MOD = 1e9 + 7;
bool X[810][810];
long long isvisit[810][810];
long long H, W, K;
void dfs(queue<pair<long long, long long>> &que, long long y, long long x,
         int move) {
  if (y >= H) return;
  if (x >= W) return;
  if (y < 0) return;
  if (x < 0) return;
  if (move == 0) return;
  if (!X[y][x]) return;
  if (isvisit[y][x] >= move) return;
  isvisit[y][x] = move;
  que.push(make_pair(y, x));
  dfs(que, y + 1, x, move - 1);
  dfs(que, y - 1, x, move - 1);
  dfs(que, y, x + 1, move - 1);
  dfs(que, y, x - 1, move - 1);
}
long long solve(pair<long long, long long> s) {
  queue<pair<long long, long long>> que;
  memset(isvisit, -1, sizeof(isvisit));
  dfs(que, s.first, s.second, K);
  long long ans = 1000;
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    long long y = p.first;
    long long x = p.second;
    long long dist = 1000;
    chmin(dist, y);
    chmin(dist, x);
    chmin(dist, H - 1 - y);
    chmin(dist, W - 1 - x);
    chmin(ans, dist / K + (dist % K != 0));
  }
  return ans + 1;
}
int main() {
  cin >> H >> W >> K;
  pair<long long, long long> S;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      if (c == 'S') {
        S = make_pair(i, j);
        X[i][j] = true;
      } else if (c == '.')
        X[i][j] = true;
    }
  }
  long long ans = solve(S);
  cout << ans << endl;
  return 0;
}
