#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int MAX = 8e2 + 5;
const long long MAX2 = 11;
const long long MOD = 1000000007;
const long long INF = 1e18;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 450;
int n, m, k, a, b, vis[MAX][MAX], nr, nc, gb;
char x[MAX][MAX];
queue<pair<int, int> > q;
inline bool inside(int r, int c) { return r > 0 && c > 0 && r <= n && c <= m; }
void dfs1(int r, int c, int mv) {
  if (!mv) return;
  for (long long i = 0; i <= 3; ++i) {
    nr = r + dr[i], nc = c + dc[i];
    if (!inside(nr, nc)) continue;
    if (vis[nr][nc] != -1) continue;
    if (x[nr][nc] == '#') continue;
    vis[nr][nc] = 1;
    q.push({nr, nc});
    dfs1(nr, nc, mv - 1);
  }
}
void dfs(int r, int c, int mv) {
  if (!mv) return;
  for (long long i = 0; i <= 3; ++i) {
    nr = r + dr[i], nc = c + dc[i];
    if (!inside(nr, nc)) continue;
    if (vis[nr][nc] != -1) continue;
    vis[nr][nc] = gb;
    q.push({nr, nc});
    dfs(nr, nc, mv - 1);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  for (long long i = 1; i <= n; ++i)
    for (long long j = 1; j <= m; ++j) {
      cin >> x[i][j];
      if (x[i][j] == 'S') a = i, b = j;
    }
  memset(vis, -1, sizeof vis);
  vis[a][b] = 1;
  q.push({a, b});
  dfs1(a, b, k);
  while (!q.empty()) {
    a = q.front().first, b = q.front().second;
    q.pop();
    if (a == 1 || a == n || b == 1 || b == m)
      return cout << vis[a][b] << endl, 0;
    gb = vis[a][b] + 1;
    dfs(a, b, k);
  }
  return 0;
}
