#include <bits/stdc++.h>
using namespace std;
int R, C, k;
char g[800][801];
char vis[800][800][408 / 8];
struct state {
  int r, c, rem;
};
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
int main() {
  scanf("%d%d%d", &R, &C, &k);
  for (int i = 0; i < R; ++i) scanf("%s", g[i]);
  queue<state> q, p;
  for (int i = 0; i < R; ++i)
    for (int j = 0; j < C; ++j)
      if (g[i][j] == 'S') {
        q.push(state{i, j, 0});
        vis[i][j][0] = 1;
        g[i][j] = '.';
        j = C;
        break;
      }
  int cost = 0;
  state s, ns;
  while (!q.empty()) {
    int remK = k;
    ++cost;
    while (remK-- && !q.empty())
      for (int it = q.size(); it > 0; --it) {
        s = q.front();
        q.pop();
        if (s.r == 0 || s.r == R - 1 || s.c == 0 || s.c == C - 1) {
          if (remK == k - 1) {
            printf("%d\n", cost - 1);
            return 0;
          }
          printf("%d\n", cost);
          return 0;
        }
        ns = s;
        ns.rem = min(400, ns.rem + k);
        if (!((vis[ns.r][ns.c][ns.rem >> 3] >> (ns.rem & 7)) & 1)) {
          vis[ns.r][ns.c][ns.rem >> 3] |= 1 << (ns.rem & 7);
          p.push(ns);
        }
        for (int i = 0; i < 4; ++i) {
          ns.r = s.r + dr[i];
          ns.c = s.c + dc[i];
          ns.rem = s.rem;
          if (g[ns.r][ns.c] == '#') {
            if (!ns.rem) continue;
            --ns.rem;
          }
          if (!((vis[ns.r][ns.c][ns.rem >> 3] >> (ns.rem & 7)) & 1)) {
            vis[ns.r][ns.c][ns.rem >> 3] |= 1 << (ns.rem & 7);
            q.push(ns);
          }
          ns.rem = min(400, ns.rem + k);
          if (!((vis[ns.r][ns.c][ns.rem >> 3] >> (ns.rem & 7)) & 1)) {
            vis[ns.r][ns.c][ns.rem >> 3] |= 1 << (ns.rem & 7);
            p.push(ns);
          }
        }
      }
    while (!q.empty()) q.pop();
    q.swap(p);
  }
  puts("-1");
  return 0;
}
