#include <bits/stdc++.h>
using namespace std;
int ans = 1001001001, H, W, K;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
char grid[1000][1000];
void dfs(int x, int y, int depth) {
  if (depth > K) return;
  int tmp = min(min(x, y), min(H - 1 - x, W - 1 - y));
  ans = min(ans, (tmp + K - 1) / K + 1);
  for (int i = 0; i < 4; i++)
    if (x + dx[i] >= 0 && x + dx[i] < H && y + dy[i] >= 0 && y + dy[i] < W &&
        grid[x + dx[i]][y + dy[i]] != '#')
      dfs(x + dx[i], y + dy[i], depth + 1);
  return;
}
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) scanf("%s", grid[i]);
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (grid[i][j] == 'S') dfs(i, j, 0);
  cout << ans << endl;
  return 0;
}
