#include <bits/stdc++.h>
using std::max;
using std::min;
const int N = 810;
int n, m, k, sx, sy;
bool map[N][N], vis[N][N];
void Init() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      char ch = getchar();
      while (ch != '.' && ch != '#' && ch != 'S') ch = getchar();
      if (ch == '#') map[i][j] = 1;
      if (ch == 'S') sx = i, sy = j;
    }
  }
}
void Dfs(int x, int y, int dep) {
  if (dep > k || vis[x][y] || map[x][y]) return;
  vis[x][y] = 1;
  if (x > 1) Dfs(x - 1, y, dep + 1);
  if (x < n) Dfs(x + 1, y, dep + 1);
  if (y > 1) Dfs(x, y - 1, dep + 1);
  if (y < m) Dfs(x, y + 1, dep + 1);
}
void Solve() {
  Dfs(sx, sy, 0);
  int dis = max(n, m);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (vis[i][j]) {
        dis = min(dis, min(min(i - 1, n - i), min(j - 1, m - j)));
      }
  printf("%d\n", (dis + k - 1) / k + 1);
}
int main() {
  Init();
  Solve();
  return 0;
}
