#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char a[802][802];
struct Coord {
  int x, y;
} st;
int dist[802][802];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
queue<Coord> q;
int ans;
int Manhattan(int x, int y) {
  if (x == 1 || x == h || y == 1 || y == w) return 0;
  int ret = min(x - 2, y - 2) / k + 1;
  ret = min(ret, min(h - x - 1, w - y - 1) / k + 1);
  return ret;
}
void Bfs() {
  q.push(st);
  while (!q.empty()) {
    Coord nod = q.front();
    q.pop();
    ans = min(ans, 1 + Manhattan(nod.x, nod.y));
    if (dist[nod.x][nod.y] >= k) return;
    for (int d = 0; d < 4; ++d)
      if (a[nod.x + dx[d]][nod.y + dy[d]] == '.' &&
          (!dist[nod.x + dx[d]][nod.y + dy[d]] ||
           dist[nod.x + dx[d]][nod.y + dy[d]] > dist[nod.x][nod.y] + 1)) {
        dist[nod.x + dx[d]][nod.y + dy[d]] = dist[nod.x][nod.y] + 1;
        q.push({nod.x + dx[d], nod.y + dy[d]});
      }
  }
}
int main() {
  scanf("%d %d %d", &h, &w, &k);
  for (int i = 1; i <= h; ++i) {
    scanf("%s", a[i] + 1);
    for (int j = 1; j <= w; ++j)
      if (a[i][j] == 'S') st = {i, j};
  }
  ans = 1000000000;
  Bfs();
  printf("%d\n", ans);
  return 0;
}
