#include <bits/stdc++.h>
const int maxn = 1e3 + 10;
const int N = 2e5 + 10;
using namespace std;
int id(int l, int r) { return l + r | l != r; }
long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); }
long long qpow(long long p, long long q) {
  long long f = 1;
  while (q) {
    if (q & 1) f = f * p % 1000000007;
    p = p * p % 1000000007;
    q >>= 1;
  }
  return f;
}
int n, m, k, t, vis[maxn][maxn], ret, dis[][2] = {0, 1, 1, 0, 0, -1, -1, 0};
queue<pair<int, int>> pq;
char a[maxn][maxn];
bool flag;
bool ok(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
void bfs1(int x, int y, int go, int now) {
  if (flag) return;
  queue<pair<pair<int, int>, int>> pq1;
  pq1.push(make_pair(make_pair(x, y), 0));
  while (!pq1.empty()) {
    pair<pair<int, int>, int> p = pq1.front();
    if (p.second == k) return;
    pq1.pop();
    for (int i = 0; i < 4; i++) {
      int tx = p.first.first + dis[i][0], ty = p.first.second + dis[i][1];
      if (ok(tx, ty) && vis[tx][ty] == -1) {
        if (go == 0 && a[tx][ty] == '#') continue;
        vis[tx][ty] = now;
        if (tx == 0 || ty == 0 || tx == n - 1 || ty == m - 1) {
          ret = vis[tx][ty];
          flag = true;
          return;
        }
        pq1.push(make_pair(make_pair(tx, ty), p.second + 1));
        pq.push(make_pair(tx, ty));
      }
    }
  }
}
void bfs(int sx, int sy) {
  vis[sx][sy] = 1;
  pq.push(make_pair(sx, sy));
  bfs1(sx, sy, 0, 1);
  while (!pq.empty()) {
    pair<int, int> x = pq.front();
    pq.pop();
    bfs1(x.first, x.second, 1, vis[x.first][x.second] + 1);
    if (flag) return;
  }
}
int main() {
  int i, j;
  memset(vis, -1, sizeof(vis));
  scanf("%d%d%d", &n, &m, &k);
  for (i = 0; i <= n - 1; i++) scanf("%s", a[i]);
  for (i = 0; i <= n - 1; i++)
    for (j = 0; j <= m - 1; j++)
      if (a[i][j] == 'S') bfs(i, j);
  printf("%d\n", ret);
  return 0;
}
