#include <bits/stdc++.h>
using namespace std;
short dp[808][808][444];
struct st {
  short y, x, d;
  st() {}
  st(short y, short x, short d) : y(y), x(x), d(d) {}
};
signed main() {
  short h, w, k;
  cin >> h >> w >> k;
  string a[h];
  for (short i = 0; i < h; i++) cin >> a[i];
  short sy, sx;
  for (short i = 0; i < h; i++) {
    for (short j = 0; j < w; j++) {
      if (a[i][j] == 'S') sy = i, sx = j;
    }
  }
  short ans = 8000;
  short ay[] = {0, 0, 1, -1, 0};
  short ax[] = {1, -1, 0, 0, 0};
  memset(dp, -1, sizeof(dp));
  queue<st> q;
  q.emplace(sy, sx, 0);
  dp[sy][sx][0] = 0;
  while (!q.empty()) {
    st p = q.front();
    q.pop();
    short y = p.y, x = p.x, d = p.d;
    if (dp[y][x][d] > 440) continue;
    if (dp[y][x][d] < 0) continue;
    for (short i = 0; i < 4; i++) {
      short ny = y + ay[i], nx = x + ax[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w) {
        ans = (dp[y][x][d] - 1) / k + 1;
        goto END;
      }
      short nd = d + (a[ny][nx] == '#');
      if (nd > 440) continue;
      if (~dp[ny][nx][nd]) continue;
      if ((dp[y][x][d] / k) * k < nd) continue;
      dp[ny][nx][nd] = dp[y][x][d] + 1;
      q.emplace(ny, nx, nd);
    }
    dp[y][x][d]++;
    q.emplace(y, x, d);
  }
END:
  cout << ans << endl;
  return 0;
}
