#include <bits/stdc++.h>
using namespace std;
int H, W, K, Sx, Sy, dis[810][810], Ans = 1e9;
bool g[810][810], vis[810][810];
void search(int x, int y, int k) {
  if (!g[x][y]) return;
  Ans = min(Ans, dis[x][y] + 1);
  if (!k) return;
  if (vis[x][y]) return;
  search(x - 1, y, k - 1);
  search(x + 1, y, k - 1);
  search(x, y - 1, k - 1);
  search(x, y + 1, k - 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;
    }
  search(Sx, Sy, K);
  printf("%d\n", Ans);
}
