#include <bits/stdc++.h>
using namespace std;
int h, w, K, ans = 1 << 30, i, j;
char s[805][805];
bool v[805][805];
void dfs(int x, int y, int d) {
  if (x < 1 || x > h || y < 1 || y > w || d > K || v[x][y] || s[x][y] == '#')
    return;
  int g = min(min(x - 1, y - 1), min(h - x, w - y));
  if (g == 0)
    ans = 1;
  else
    ans = min(ans, (g - 1) / K + 2);
  v[x][y] = true;
  dfs(x, y - 1, d + 1);
  dfs(x, y + 1, d + 1);
  dfs(x - 1, y, d + 1);
  dfs(x + 1, y, d + 1);
}
int main() {
  scanf("%d%d%d", &h, &w, &K);
  for (i = 1; i <= h; ++i) scanf("%s", s[i] + 1);
  for (i = 1; i <= h; ++i) {
    for (j = 1; j <= w; ++j)
      if (s[i][j] == 'S') break;
    if (j <= w) break;
  }
  dfs(i, j, 0);
  printf("%d", ans);
  return 0;
}
