#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
int sgn(double x) { return x < -eps ? -1 : x > eps; }
int Mn(int& a, int b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int Mx(int& a, int b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); }
long long inv(long long a, long long b) {
  return a == 1 ? 1 : (1 - b * inv(b % a, a)) / a;
}
long long exg(long long a, long long b, int& x, int& y) {
  !a ? x = 0, y = 1 : (exg(b % a, a, y, x), x -= b / a * y);
}
long long pow(long long a, long long b, long long M) {
  long long t = 1;
  for (a %= M; b; b >>= 1) {
    if (b & 1) t = t * a % M;
    a = a * a % M;
  }
  return t;
}
const int N = 805;
char a[N][N];
int n, m, k, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}, ans = 1e9, f[N][N];
int dfs(int x, int y) {
  if (x == 1 || y == 1 || x == n || y == m) {
    ans = 1;
    return 0;
  }
  Mn(ans, 1 + (x - 2) / k + 1);
  Mn(ans, 1 + (y - 2) / k + 1);
  Mn(ans, 1 + (n - x - 1) / k + 1);
  Mn(ans, 1 + (m - y - 1) / k + 1);
  if (f[x][y] == k) return 0;
  for (int i = 0; i <= int(3); i++) {
    int px = x + dx[i], py = y + dy[i];
    if (a[px][py] == '.' && Mn(f[px][py], f[x][y] + 1)) dfs(px, py);
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= int(n); i++) scanf("%s", &a[i][1]);
  memset(f, 0x3f, sizeof(f));
  for (int i = 1; i <= int(n); i++)
    for (int j = 1; j <= int(m); j++)
      if (a[i][j] == 'S') f[i][j] = 0, dfs(i, j);
  cout << ans << endl;
}
