#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 dfs(int x, int y, int cnt, int go, int now) {
  if (flag || cnt == k) return;
  for (int i = 0; i < 4; i++) {
    int to_x = x + dis[i][0], to_y = y + dis[i][1];
    if (ok(to_x, to_y) && vis[to_x][to_y] == -1) {
      if (a[to_x][to_y] == '#' && go == 0) continue;
      if (to_x == 0 || to_x == n - 1 || to_y == 0 || to_y == m - 1) {
        flag = true;
        ret = now;
        return;
      }
      vis[to_x][to_y] = now;
      dfs(to_x, to_y, cnt + 1, go, now);
      pq.push(make_pair(to_x, to_y));
    }
  }
}
void bfs(int sx, int sy) {
  vis[sx][sy] = 1;
  pq.push(make_pair(sx, sy));
  dfs(sx, sy, 0, 0, 1);
  while (!pq.empty()) {
    pair<int, int> x = pq.front();
    pq.pop();
    dfs(x.first, x.second, 0, 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;
}
