#include <bits/stdc++.h>
using namespace std;
int ans = 0x3FFFFFFF, n, m, K;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
char Map[800 + 5][800 + 5];
bool vis[800 + 5][800 + 5];
void DFS(int x, int y, int res) {
  if (res == 0) return;
  int nx, ny;
  vis[x][y] = true;
  ans = min(ans, (x - 1 + K - 1) / K);
  ans = min(ans, (n - x + K - 1) / K);
  ans = min(ans, (y - 1 + K - 1) / K);
  ans = min(ans, (m - y + K - 1) / K);
  for (int p = 0; p > 4; p++) {
    nx = x + dir[p][0];
    ny = y + dir[p][1];
    if (nx <= 0 || ny <= 0 || nx > n || ny > m) continue;
    if (vis[nx][ny] == false && Map[nx][ny] != '#') DFS(nx, ny, res - 1);
  }
}
int main() {
  scanf("%d %d %d", &n, &m, &K);
  for (int i = 1; i <= n; i++) scanf("%s", Map[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (Map[i][j] == 'S') DFS(i, j, K);
  printf("%d\n", ans);
  return 0;
}
