#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long MOD = 1e9 + 7;
bool X[810][810];
pair<long long, long long> isvisit[810][810];
long long H, W, K;
bool dfs(queue<pair<long long, long long>> &que, long long y, long long x,
         int num, int move) {
  if (y >= H) return false;
  if (x >= W) return false;
  if (y < 0) return false;
  if (x < 0) return false;
  if (!X[y][x]) {
    if (num > 0)
      num--;
    else {
      que.push(make_pair(y, x));
      return false;
    }
  }
  auto p = isvisit[y][x];
  if (p.first >= num && p.second >= move) return false;
  isvisit[y][x] = make_pair(num, move);
  if (y == 0) return true;
  if (x == 0) return true;
  if (y == H - 1) return true;
  if (x == W - 1) return true;
  bool ret = false;
  chmax(ret, dfs(que, y + 1, x, num, move - 1));
  chmax(ret, dfs(que, y - 1, x, num, move - 1));
  chmax(ret, dfs(que, y, x + 1, num, move - 1));
  chmax(ret, dfs(que, y, x - 1, num, move - 1));
  return ret;
}
long long solve(pair<long long, long long> s) {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      isvisit[i][j] = make_pair(-1, -1);
    }
  }
  queue<pair<long long, long long>> tmp;
  if (dfs(tmp, s.first, s.second, 0, K)) return 1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      isvisit[i][j] = make_pair(-1, -1);
    }
  }
  queue<pair<long long, long long>> que[2];
  int num = 1;
  que[0].push(s);
  int idx = 0;
  while (1) {
    num++;
    while (!que[idx % 2].empty()) {
      auto p = que[idx % 2].front();
      que[idx % 2].pop();
      bool end = dfs(que[(idx + 1) % 2], p.first, p.second, K, K);
      if (end) return num;
    }
    idx++;
  }
}
int main() {
  cin >> H >> W >> K;
  pair<long long, long long> S;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      if (c == 'S') {
        S = make_pair(i, j);
        X[i][j] = true;
      } else if (c == '.')
        X[i][j] = true;
    }
  }
  long long ans = solve(S);
  cout << ans << endl;
  return 0;
}
