#include <bits/stdc++.h>
using namespace std;
const int maxn = 801, inf = 1e9;
int dp[maxn][maxn], dis[maxn][maxn], mp[maxn][maxn], h, w, k;
queue<pair<int, int> > q;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
bool ok(pair<int, int> x) {
  return ((x.first == 0 || x.first == w - 1) ||
          (x.second == 0 || x.second == h - 1));
}
bool valid(pair<int, int> x) {
  return (x.first < w && x.second < h && x.first >= 0 && x.second >= 0);
}
pair<int, int> mv(pair<int, int> x, int i) {
  return pair<int, int>(x.first + dx[i], x.second + dy[i]);
}
void smin(int &a, int b) {
  if (a > b) a = b;
}
int main() {
  scanf("%d%d%d", &w, &h, &k);
  memset(dis, 63, sizeof dis);
  memset(dp, 63, sizeof dp);
  for (int i = 0; i < w; i++)
    for (int j = 0; j < h; j++) {
      char c;
      scanf(" %c", &c);
      if (c == '#') mp[i][j] = 1;
      if (c == 'S') {
        q.push(pair<int, int>(i, j));
        dis[i][j] = 0;
      }
    }
  int ans = 1;
  while ((int)q.size()) {
    pair<int, int> v = q.front();
    q.pop();
    int d = dis[v.first][v.second];
    dp[v.first][v.second] = 0;
    if (d == k) continue;
    if (ok(v)) {
      puts("1");
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      pair<int, int> nx = mv(v, i);
      if (valid(nx) && dis[nx.first][nx.second] >= inf &&
          mp[nx.first][nx.second] == 0) {
        dis[nx.first][nx.second] = d + 1;
        q.push(nx);
      }
    }
  }
  if (k > min(w, h) / 2) {
    puts("2");
    return 0;
  }
  while (true) {
    ans++;
    for (int _ = 0; _ < k; _++) {
      for (int i = 0; i < w; i++)
        for (int j = 0; j < h; j++)
          for (int it = 0; it < 4; it++) {
            pair<int, int> nx = mv(pair<int, int>(i, j), it);
            if (!valid(nx)) continue;
            if (dp[i][j] + mp[nx.first][nx.second] <= k) {
              smin(dp[nx.first][nx.second], dp[i][j] + mp[nx.first][nx.second]);
            }
          }
    }
    for (int i = 0; i < w; i++)
      for (int j = 0; j < h; j++) {
        if (dp[i][j] >= inf) continue;
        dp[i][j] = 0;
        if (ok(pair<int, int>(i, j))) {
          printf("%d\n", ans);
          return 0;
        }
      }
  }
}
