#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, cost;
  edge(int t, int c) : to(t), cost(c) {}
};
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int H, W, K;
string A[800];
int sx, sy;
bool used[800][800];
bool ok(int x, int y) {
  return (x >= 0 && x < W && y >= 0 && y < H && A[y][x] == '.' && !used[y][x]);
}
int calc(int y, int x) { return min(min(x, y), min(W - x - 1, H - y - 1)); }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W >> K;
  for (int i = (0); i < (H); ++i) {
    cin >> A[i];
    for (int j = (0); j < (W); ++j)
      if (A[i][j] == 'S') sx = j, sy = i;
  }
  queue<pair<int, int> > Q;
  Q.push(pair<int, int>(sx, sy));
  used[sy][sx] = true;
  for (int i = (0); i < (K + 1); ++i) {
    int sz = Q.size();
    for (int j = (0); j < (sz); ++j) {
      pair<int, int> p = Q.front();
      Q.pop();
      for (int d = (0); d < (4); ++d) {
        int nx = p.first + dx[d], ny = p.second + dy[d];
        if (ok(nx, ny)) {
          used[ny][nx] = true;
          Q.push(pair<int, int>(nx, ny));
        }
      }
    }
  }
  int ans = 1.1e9;
  for (int i = (0); i < (H); ++i) {
    for (int j = (0); j < (W); ++j) {
      if (used[i][j]) ans = min(ans, calc(i, j));
    }
  }
  cout << (ans / K) + !!(ans % K) + 1 << endl;
  return 0;
}
