#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y, B[805][805], ans = (1 << 30);
char A[805][805];
void flood(int x, int y, int d) {
  if (d > k) return;
  B[x][y] = 1;
  if (x > 1 && !B[x - 1][y] && A[x - 1][y] != '#') flood(x - 1, y, d + 1);
  if (y > 1 && !B[x][y - 1] && A[x][y - 1] != '#') flood(x, y - 1, d + 1);
  if (x < n && !B[x + 1][y] && A[x + 1][y] != '#') flood(x + 1, y, d + 1);
  if (y < m && !B[x][y + 1] && A[x][y + 1] != '#') flood(x, y + 1, d + 1);
}
int main() {
  int i, j;
  scanf("%d %d %d\n", &n, &m, &k);
  for (i = 1; i <= n; i++) scanf("%s", A[i] + 1);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if (A[i][j] == 'S') x = i, y = j;
  flood(x, y, 0);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if (B[i][j])
        ans = min((min(min(j - 1, m - j), min(i - 1, n - i)) + k - 1) / k + 1,
                  ans);
  cout << ans;
  return 0;
}
