#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long longinf = 1LL << 60;
const long long mod = 1e9 + 7;
vector<long long> dist;
bool bellman_ford(int n, int s, vector<vector<long long>>& cost) {
  dist = vector<long long>(n, longinf);
  dist[s] = 0;
  for (int i = (int)(0); i < (int)(n); i++) {
    for (int j = (int)(0); j < (int)(n); j++) {
      for (int k = (int)(0); k < (int)(n); k++) {
        if (j != k && dist[j] != longinf && dist[k] > dist[j] + cost[j][k]) {
          dist[k] = dist[j] + cost[j][k];
          if (i == n - 1) return true;
        }
      }
    }
  }
  return false;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for (int i = (int)(0); i < (int)(H); i++) cin >> A[i];
  vector<vector<long long>> cost(H * W, vector<long long>(H * W, longinf));
  int dx[] = {0, -1, 0, 1};
  int dy[] = {-1, 0, 1, 0};
  int s;
  for (int i = (int)(0); i < (int)(H); i++)
    for (int j = (int)(0); j < (int)(W); j++) {
      if (A[i][j] == 'S') s = i * W + j;
      for (int k = (int)(0); k < (int)(4); k++) {
        int nx = i + dx[k];
        int ny = j + dy[k];
        if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
        cost[i * W + j][nx * W + ny] = (A[nx][ny] == '#');
      }
    }
  long long ans = longinf;
  auto bsearch = [&]() {
    auto test = [&](int k) {
      for (int i = (int)(0); i < (int)(H); i++)
        if (dist[i * W] <= K * (k - 1) || dist[i * W + W - 1] <= K * (k - 1))
          return true;
      for (int j = (int)(0); j < (int)(W); j++)
        if (dist[j] <= K * (k - 1) || dist[(H - 1) * W + j] <= K * (k - 1))
          return true;
      return false;
    };
    long long ok = H * W + 1, ng = -1;
    while (ok - ng > 1) {
      long long mid = (ok + ng) / 2;
      (test(mid) ? ok : ng) = mid;
    }
    return ok;
  };
  function<void(int, int, int)> dfs = [&](int cur, int par, int d) {
    if (d > K) return;
    bellman_ford(H * W, cur, cost);
    ans = min(bsearch(), ans);
    int i = cur / W;
    int j = cur % W;
    for (int k = (int)(0); k < (int)(4); k++) {
      int nx = i + dx[k];
      int ny = j + dy[k];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
      if (nx * W + ny == par) continue;
      if (A[nx][ny] == '.') dfs(nx * W + ny, cur, d + 1);
    }
  };
  dfs(s, -1, 0);
  cout << ans << "\n";
  return 0;
}
