#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int maxr = 1100;
const int maxc = 1100;
char str[maxc];
int n, m, K;
int a[maxr][maxc], sx, sy;
int d[maxr][maxc];
void dfs(const int x, const int y, const int k) {
  if (d[x][y] <= k || k >= K) return;
  d[x][y] = k;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (a[nx][ny]) dfs(nx, ny, k + 1);
  }
}
int main() {
  memset(d, 63, sizeof d);
  scanf("%d%d%d", &n, &m, &K);
  for (int i = 1; i <= n; i++) {
    scanf("%s", str);
    for (int j = 1; j <= m; j++) {
      if (str[j - 1] == '#')
        a[i][j] = 0;
      else
        a[i][j] = 1;
      if (str[j - 1] == 'S') sx = i, sy = j;
    }
  }
  dfs(sx, sy, 0);
  int ans = INT_MAX;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (d[i][j] <= K) {
        int x = 1 + (min(i - 1, n - i) + K - 1) / K;
        int y = 1 + (min(j - 1, m - j) + K - 1) / K;
        ans = min(ans, min(x, y));
      }
  printf("%d\n", ans);
  return 0;
}
