#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char s[1005][1005];
int vis[1005][1005];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
  scanf("%d%d%d", &n, &m, &k);
  int x, y;
  memset(vis, -1, sizeof(vis));
  for (int i = 0; i < n; ++i) {
    scanf("%s", s[i]);
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == 'S') {
        x = i;
        y = j;
        s[i][j] = '.';
      }
    }
  }
  queue<pair<int, int>> q;
  q.push(make_pair(x, y));
  vis[x][y] = 0;
  while (!q.empty()) {
    auto w = q.front();
    q.pop();
    if (vis[w.first][w.second] == k) continue;
    for (int i = 0; i < 4; ++i) {
      int nx = w.first + dx[i];
      int ny = w.second + dy[i];
      if (nx < 0 || ny < 0 || nx >= n || ny >= m) {
        printf("1\n");
        return 0;
      }
      if (s[nx][ny] == '#' || vis[nx][ny] != -1) continue;
      vis[nx][ny] = vis[w.first][w.second] + 1;
    }
  }
  int ans = 999999;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (vis[i][j] == -1) continue;
      int dis = min(i, j);
      dis = min(dis, n - i - 1);
      dis = min(dis, m - j - 1);
      dis = dis / k + (dis % k != 0);
      ans = min(ans, 1 + dis);
    }
  }
  printf("%d\n", ans);
  return 0;
}
