#include <bits/stdc++.h>
using namespace std;
int H, W, K, Sx, Sy, dis[810][810], dep[810][810], Ans = 1e9;
bool g[810][810], vis[810][810];
queue<pair<int, int> > q;
void BFS() {
  while (!q.empty()) q.pop();
  vis[Sx][Sy] = 1;
  q.push(make_pair(Sx, Sy));
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    Ans = min(Ans, dis[x][y] + 1);
    if (dep[x][y] == K) continue;
    if (g[x - 1][y] && !vis[x - 1][y])
      vis[x - 1][y] = 1, q.push(make_pair(x - 1, y));
    if (g[x + 1][y] && !vis[x + 1][y])
      vis[x + 1][y] = 1, q.push(make_pair(x + 1, y));
    if (g[x][y - 1] && !vis[x][y - 1])
      vis[x][y - 1] = 1, q.push(make_pair(x, y - 1));
    if (g[x][y + 1] && !vis[x][y + 1])
      vis[x][y + 1] = 1, q.push(make_pair(x, y + 1));
  }
}
int main() {
  int i, j;
  char c;
  scanf("%d%d%d", &H, &W, &K);
  for (i = 1; i <= H; i++)
    for (j = 1; j <= W; j++) {
      c = getchar();
      while (c != '.' && c != '#' && c != 'S') c = getchar();
      if (c == '#') continue;
      g[i][j] = 1;
      dis[i][j] = min(min((i - 1 + K - 1) / K, (j - 1 + K - 1) / K),
                      min((H - i + K - 1) / K, (W - j + K - 1) / K));
      if (c == 'S') Sx = i, Sy = j;
    }
  BFS();
  printf("%d\n", Ans);
}
