#include <bits/stdc++.h>
using namespace std;
struct cell {
  int x, y;
  cell(int x = 0, int y = 0) : x(x), y(y) {}
};
int H, W, K, d[810][810], sz, res;
char a[810][810];
bool v[810][810];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, -1, 1};
void dfs(int x, int y, int dist) {
  int k, nx, ny;
  v[x][y] = 1;
  res = min(res, 1 + min({(x - 1 + K - 1) / K, (H - x + K - 1) / K,
                          (y - 1 + K - 1) / K, (W - y + K - 1) / K}));
  if (dist == K) return;
  for (k = 0; k < 4; k++) {
    nx = x + dx[k], ny = y + dy[k];
    if (nx < 1 || nx > H || ny < 1 || ny > W || a[nx][ny] == '#' || v[nx][ny])
      continue;
    dfs(nx, ny, dist + 1);
  }
}
int main() {
  cin >> H >> W >> K;
  int i, j, sx, sy;
  for (i = 1; i <= H; i++)
    for (j = 1; j <= W; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') sx = i, sy = j, d[i][j] = 0;
    }
  res = (1 << 30);
  dfs(sx, sy, 0);
  cout << res << "\n";
  return 0;
}
