#include <bits/stdc++.h>
using namespace std;
int n, m, k, sx, sy;
int a[1010][1010];
bool visit[1010][1010];
void bfs() {
  static queue<pair<int, int> > Q;
  Q.push(make_pair(sx, sy));
  visit[sx][sy] = 1;
  while (!Q.empty()) {
    int tx = Q.front().first, ty = Q.front().second;
    Q.pop();
    if (tx > 1 && !visit[tx - 1][ty] && !a[tx - 1][ty]) {
      Q.push(make_pair(tx - 1, ty));
      visit[tx - 1][ty] = 1;
    }
    if (ty > 1 && !visit[tx][ty - 1] && !a[tx][ty - 1]) {
      Q.push(make_pair(tx, ty - 1));
      visit[tx][ty - 1] = 1;
    }
    if (tx < n && !visit[tx + 1][ty] && !a[tx + 1][ty]) {
      Q.push(make_pair(tx + 1, ty));
      visit[tx + 1][ty] = 1;
    }
    if (ty < m && !visit[tx][ty + 1] && !a[tx][ty + 1]) {
      Q.push(make_pair(tx, ty + 1));
      visit[tx][ty + 1] = 1;
    }
  }
}
int dis(int x, int y) {
  int res = min(min(x - 1, n - x), min(y - 1, m - y));
  if (!res) return 0;
  return (res - 1) / k + 1;
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    static char str[1010];
    scanf("%s", str + 1);
    for (int j = 1; j <= m; j++) {
      if (str[j] == '#') a[i][j] = 1;
      if (str[j] == 'S') sx = i, sy = j;
    }
  }
  bfs();
  int ans = 0x7FFFFFFF;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (visit[i][j]) ans = min(ans, dis(i, j) + 1);
  printf("%d\n", ans);
  return 0;
}
