#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char s[1000][1000];
bool vis[1000][1000];
void dfs(int x, int y, int d) {
  if (x < 1 || y < 1 || x > n || y > m || d > k || vis[x][y] || s[x][y] == '#')
    return;
  vis[x][y] = 1;
  dfs(x - 1, y, d + 1);
  dfs(x, y - 1, d + 1);
  dfs(x, y + 1, d + 1);
  dfs(x + 1, y, d + 1);
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  memset(vis, 0, sizeof vis);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[i][j] == 'S') dfs(i, j, 0);
  int ans = 1 << 30;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (vis[i][j])
        ans = min(
            ans,
            int(ceil(min<double>(min(i - 1, n - i), min(j - 1, m - j)) / k)) +
                1);
  printf("%d\n", ans);
  return 0;
}
