#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265358979323846264338327950288;
char a[800][800];
const long long inf = 99999999999999;
struct A {
  long long cost, magic, rest;
};
struct Q {
  long long x, y;
};
A b[800][800];
long long u1[4] = {0, 0, -1, 1}, u2[4] = {1, -1, 0, 0};
signed main() {
  long long h, w, x, y, k;
  cin >> h >> w >> k;
  for (long long i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (long long j = 0; j < w; j++) {
      a[i][j] = s[j];
      b[i][j] = A{inf, 0, 0};
      if (a[i][j] == 'S') y = i, x = j;
    }
  }
  b[y][x].cost = 0;
  queue<Q> q;
  q.push(Q{x, y});
  while (!q.empty()) {
    Q t = q.front();
    q.pop();
    for (long long z = 0; z < 4; z++) {
      long long x = t.x + u1[z], y = t.y + u2[z];
      if (x < w && x >= 0 && y < h && y >= 0 && a[y][x] == '.' &&
          b[t.y][t.x].cost + 1 < b[y][x].cost) {
        b[y][x].cost = b[t.y][t.x].cost + 1;
        q.push(Q{x, y});
      }
    }
  }
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      if (a[i][j] == '.') {
        b[i][j].magic = ceil((b[i][j].cost * 1.0) / k);
        b[i][j].rest = k * ceil(b[i][j].cost * 1.0 / k) - b[i][j].cost;
      }
    }
  }
  long long ans = inf;
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      if (a[i][j] == '.' || a[i][j] == 'S') {
        long long sum = min(min((h - 1) - i, i), min((w - 1) - j, j));
        if (a[i + 1][j] == '#' && a[i - 1][j] == '#' && a[i][j + 1] == '#' &&
            a[i][j - 1] == '#') {
          ans = min(ans,
                    b[i][j].magic + (long long)(1.0 + ceil((sum * 1.0) / k)));
        } else {
          ans = min(
              ans, b[i][j].magic +
                       max((long long)0,
                           max((long long)ceil((sum - b[i][j].rest) * 1.0 / k),
                               (long long)ceil((sum - b[i][j].magic * k) * 1.0 /
                                               k))));
        }
      }
    }
  }
  cout << ans << endl;
  cin >> h;
}
