#include <bits/stdc++.h>
using namespace std;
int h, w, K;
char a[805][805];
int dist[805][805];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int boundary(int x, int y) {
  if (x == 1 || x == h || y == 1 || y == w) return 1;
  return 0;
}
int valid(int x, int y) { return (x >= 1 && x <= h && y >= 1 && y <= w); }
int blocked(int x, int y) { return (a[x][y] == '#'); }
int ceiling(int x, int y) {
  if (x % y) return x / y + 1;
  return x / y;
}
int main() {
  ios::sync_with_stdio(false);
  ;
  cin.tie(0);
  ;
  int i, j, sx, sy, x, y, z, nxtx, nxty, curx, cury, dis, steps, blocks;
  string s;
  cin >> h >> w >> K;
  for (i = 1; i <= h; i++) {
    cin >> s;
    s = " " + s;
    for (j = 1; j <= w; j++) a[i][j] = s[j];
  }
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) dist[i][j] = 10000000;
  }
  priority_queue<pair<int, pair<pair<int, int>, pair<int, int> > > > q;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (!boundary(i, j)) continue;
      if (!blocked(i, j))
        dist[i][j] = 0;
      else
        dist[i][j] = 1;
      if (!blocked(i, j))
        q.push({0, {{0, 0}, {i, j}}});
      else
        q.push({-1, {{0, 1}, {i, j}}});
    }
  }
  while (!q.empty()) {
    dis = -q.top().first;
    steps = q.top().second.first.first;
    blocks = q.top().second.first.second;
    curx = q.top().second.second.first;
    cury = q.top().second.second.second;
    q.pop();
    if (dist[curx][cury] != dis) continue;
    for (i = 0; i < 4; i++) {
      nxtx = curx + dx[i];
      nxty = cury + dy[i];
      if (!valid(nxtx, nxty)) continue;
      x = steps + 1;
      y = blocks;
      if (blocked(nxtx, nxty)) y++;
      z = max(ceiling(x, K), ceiling(y, K));
      if (dist[nxtx][nxty] > z) {
        dist[nxtx][nxty] = z;
        q.push({-dist[nxtx][nxty], {{x, y}, {nxtx, nxty}}});
      }
    }
  }
  cout << dist[sx][sy];
  return 0;
}
