#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double EPS = numeric_limits<double>::epsilon();
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const int MOD = (int)1e9 + 7;
int h, w, k;
const int M = 805;
char g[M][M];
int c1[M][M];
int c2[M][M];
bool v[M][M];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
pair<int, int> dfs(int i, int j, int x1, int x2) {
  if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
    return make_pair(x1, x2);
  }
  if (x1 > k || x2 > k) {
    return make_pair(INF, INF);
  }
  if (v[i][j]) {
    return make_pair(c1[i][j], c2[i][j]);
  }
  pair<int, int> ret = make_pair(INF, INF);
  for (int k = 0; k < 4; k++) {
    int ni = i + dx[k], nj = j + dy[k];
    if (g[ni][nj] == '#') {
      pair<int, int> p = dfs(ni, nj, x1, x2 + 1);
      if (p.first + max(0, p.second - p.first) + p.second <
          ret.first + max(0, ret.second - ret.first) + ret.second) {
        ret = p;
      }
    } else {
      pair<int, int> p = dfs(ni, nj, x1 + 1, x2);
      if (p.first + max(0, p.second - p.first) + p.second <
          ret.first + max(0, ret.second - ret.first) + ret.second) {
        ret = p;
      }
    }
  }
  return ret;
}
int main() {
  scanf("%d", &h), scanf("%d", &w), scanf("%d", &k);
  for (int i = 0; i < h; i++) {
    scanf("%s", g[i]);
  }
  int si, sj;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      v[i][j] = 0;
      c1[i][j] = 0;
      c2[i][j] = 0;
      if (g[i][j] == 'S') {
        si = i, sj = j;
        g[i][j] = '.';
      }
    }
  }
  pair<int, int> p = dfs(si, sj, 0, 0);
  if (p.first == INF) {
    puts("-1");
  } else {
    printf("%d\n", p.first + max(0, p.second - p.first) + p.second);
  }
  return 0;
}
