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