#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long POW(long long a, long long b, long long MMM = MOD) {
  long long ret = 1;
  for (; b; b >>= 1, a = (a * a) % MMM)
    if (b & 1) ret = (ret * a) % MMM;
  return ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) {
  if (a == 0 || b == 0) return a + b;
  return a * (b / gcd(a, b));
}
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
char s[800][801];
int d[800][800];
int main() {
  memset((d), -1, sizeof(d));
  ;
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (int(i) = (0); (i) <= (n - 1); (i) += (1)) scanf("%s", s[i]);
  int x, y;
  for (int(i) = (0); (i) <= (n - 1); (i) += (1))
    for (int(j) = (0); (j) <= (m - 1); (j) += (1))
      if (s[i][j] == 'S') x = i, y = j;
  d[x][y] = 0;
  queue<pair<int, int> > q;
  q.push(make_pair(x, y));
  while (!q.empty()) {
    int xx, yy;
    tie(xx, yy) = q.front();
    q.pop();
    for (int(i) = (0); (i) <= (3); (i) += (1)) {
      int nx = xx + dx[i], ny = yy + dy[i];
      if (nx >= 0 && nx < n && ny >= 0 && ny < m && s[nx][ny] != '#' &&
          d[nx][ny] == -1) {
        d[nx][ny] = d[x][y] + 1;
        q.push(make_pair(nx, ny));
      }
    }
  }
  int ans = 987654321;
  for (int(i) = (0); (i) <= (n - 1); (i) += (1))
    for (int(j) = (0); (j) <= (m - 1); (j) += (1)) {
      if (d[i][j] != -1 && d[i][j] <= k) {
        int t = min({i, n - i - 1, j, m - j - 1});
        if (t > 0) t = (t - 1) / k + 1;
        ans = min(ans, t);
      }
    }
  printf("%d", ans + 1);
}
