#include <bits/stdc++.h>
using namespace std;
int ans = 200000000;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int H, W, K;
string S[810];
int bfs(int x, int y) {
  int d[810][810];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) d[i][j] = 200000000;
  d[x][y] = 0;
  queue<pair<int, int> > que;
  que.push({x, y});
  int Ld = x;
  int Rd = H - x - 1;
  int Ud = y;
  int Dd = W - y - 1;
  int tmp = (min({Ld, Rd, Ud, Dd}) + K - 1) / K;
  ans = min(ans, tmp + 1);
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    int x = p.first;
    int y = p.second;
    for (int k = 0; k < 4; k++) {
      int xx = x + dx[k];
      int yy = y + dy[k];
      if (xx < 0 || xx >= H || yy < 0 || yy >= W) continue;
      if (S[xx][yy] == '#') continue;
      if (d[xx][yy] != 200000000) continue;
      if (xx == 0 || xx == H - 1 || yy == 0 || yy == W - 1) ans = 1;
      d[xx][yy] = d[x][y] + 1;
      int Ld = xx;
      int Rd = H - xx - 1;
      int Ud = yy;
      int Dd = W - yy - 1;
      int tmp = (min({Ld, Rd, Ud, Dd}) + K - 1) / K;
      ans = min(ans, tmp + 1);
      if (d[xx][yy] < K) {
        que.push({xx, yy});
      }
    }
  }
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W >> K;
  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') bfs(i, j);
    }
  }
  cout << ans << endl;
  return 0;
}
