#include <bits/stdc++.h>
char mat[800 + 1][800 + 1];
struct Cell {
  short l;
  short c;
  int cnt;
  int step;
  int ans;
} q[(1 << 21)];
int dl[] = {-1, 0, 1, 0}, dc[] = {0, -1, 0, 1};
int n, m;
inline bool check(int l, int c) {
  if (l == 1 || l == n || c == 1 || c == m) return 1;
  return 0;
}
int main() {
  FILE *fi, *fout;
  int i, k, b, e, l, c, j;
  fi = stdin;
  fout = stdout;
  fscanf(fi, "%d %d %d ", &n, &m, &k);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      mat[i][j] = fgetc(fi);
      if (mat[i][j] == 'S') {
        l = i;
        c = j;
      }
    }
    fgetc(fi);
  }
  int ans;
  q[0].l = l;
  q[0].c = c;
  q[0].cnt = 0;
  q[0].step = k;
  q[0].ans = 0;
  b = 0;
  e = 1;
  bool flag = 1;
  while (b != e && flag) {
    if (q[b].step == 0) {
      q[e].step = k;
      q[e].ans = q[b].ans + 1;
      q[e].l = q[b].l;
      q[e].c = q[b].c;
      q[e].cnt += k;
      e++;
    } else {
      for (i = 0; i < 4 && flag; i++)
        if (mat[q[b].l + dl[i]][q[b].c + dc[i]] == '.') {
          q[e].step = q[b].step - 1;
          q[e].l = q[b].l + dl[i];
          q[e].c = q[b].c + dc[i];
          q[e].ans = q[b].ans;
          q[e].cnt = q[b].cnt;
          if (check(q[e].l, q[e].c)) {
            ans = q[e].ans;
            flag = 0;
          }
          e++;
          e &= ((1 << 21) - 1);
        }
      for (i = 0; i < 4 && flag; i++)
        if (mat[q[b].l + dl[i]][q[b].c + dc[i]] == '#') {
          if (q[b].cnt == 0) {
            q[e].step = q[b].step - 1 + k;
            q[e].l = q[b].l + dl[i];
            q[e].c = q[b].c + dc[i];
            q[e].ans = q[b].ans + 1;
            q[e].cnt = q[b].cnt + k - 1;
            if (check(q[e].l, q[e].c)) {
              ans = q[e].ans;
              flag = 0;
            }
          } else {
            q[e].step = q[b].step - 1;
            q[e].l = q[b].l + dl[i];
            q[e].c = q[b].c + dc[i];
            q[e].ans = q[b].ans;
            q[e].cnt = q[b].cnt - 1;
            if (check(q[e].l, q[e].c)) {
              ans = q[e].ans;
              flag = 0;
            }
          }
          e++;
          e &= ((1 << 21) - 1);
        }
    }
    b++;
    b &= ((1 << 21) - 1);
  }
  fprintf(fout, "%d\n", ans + 1);
  return 0;
}
