#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
inline int read() {
  int ans = 0;
  bool f = 1;
  char ch = getchar();
  while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
  while (isdigit(ch)) ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = getchar();
  return f ? ans : -ans;
}
int ans = 2147483647;
int H, W, K;
char graph[850][850];
int sx = -1, sy = -1;
int d[850][850];
queue<pair<int, int> > q;
int dist(int x) { return ((x + K - 1) / K); }
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) cin >> graph[i];
  for (int i = 0; i < H && sx == -1 && sy == -1; i++)
    for (int j = 0; j < W; j++)
      if (graph[i][j] == 'S') {
        sx = i, sy = j;
        break;
      }
  q.push(make_pair(sx, sy));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) d[i][j] = 2147483647;
  d[sx][sy] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    int nowdist = d[x][y];
    q.pop();
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (xx < 0 || yy < 0 || xx >= H || yy >= W || d[xx][yy] != 2147483647 ||
          graph[xx][yy] == '#')
        continue;
      d[xx][yy] = nowdist + 1;
      q.push(make_pair(xx, yy));
    }
  }
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      if (d[i][j] > K) continue;
      int res = 2147483647;
      res = min(res, dist(i));
      res = min(res, dist(j));
      res = min(res, dist(H - i - 1));
      res = min(res, dist(W - i - 1));
      ans = min(ans, res + 1);
    }
  cout << ans;
  return 0;
}
