#include <bits/stdc++.h>
using namespace std;
struct ini {
  int x, y;
  int d, l;
  int b;
  ini() {}
  ini(int x, int y, int d, int l, int b) : x(x), y(y), d(d), l(l), b(b) {}
  bool operator<(const ini& in) const {
    if (d > in.d) return 1;
    if (d < in.d) return 0;
    if (l > in.d) return 1;
    if (l < in.d) return 0;
    if (b < in.d) return 1;
    if (b > in.d) return 0;
    return 0;
  }
};
int h, w, k;
char c[807][807];
int dis[807][807];
int myx, myy;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void dij() {
  priority_queue<ini> pq;
  memset((void*)dis, 0x7f, sizeof(dis));
  pq.push(ini(myx, myy, 1, 0, 0));
  dis[myx][myy] = 0;
  while (((int)(pq).size())) {
    ini in = pq.top();
    pq.pop();
    for (int i = 0; i < 4; i++) {
      int nx = in.x + dx[i], ny = in.y + dy[i];
      if (nx >= 1 && nx <= h && ny >= 1 && ny <= w) {
        int nd = in.d, nl = in.l + 1, nb = in.b;
        if (c[nx][ny] == '#') nb--;
        if (nb == -1) {
          nb = k - 1;
          nl = 1;
          nd++;
        }
        if (nl == k + 1) {
          nb = k - 1;
          nl = 1;
          nd++;
        }
        if (nd < dis[nx][ny]) {
          dis[nx][ny] = nd;
          pq.push(ini(nx, ny, nd, nl, nb));
        }
      }
    }
  }
}
int main() {
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 1; i <= h; i++) {
    scanf("%s", c[i] + 1);
    for (int j = 1; j <= w; j++) {
      if (c[i][j] == 'S') {
        myx = i, myy = j;
        c[i][j] = '.';
      }
    }
  }
  dij();
  int minv = ((int)0x7f7f7f7f);
  for (int i = 1; i <= h; i++) {
    minv = min(minv, dis[i][1]);
    minv = min(minv, dis[i][w]);
  }
  for (int i = 1; i <= w; i++) {
    minv = min(minv, dis[1][i]);
    minv = min(minv, dis[h][i]);
  }
  printf("%d\n", minv);
  return 0;
}
