#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
const long long INF = 1e18;
long long power(long long x, long long y) {
  long long t = 1;
  while (y > 0) {
    if (y % 2)
      y -= 1, t = t * x % mod;
    else
      y /= 2, x = x * x % mod;
  }
  return t;
}
bool mk[805][805];
queue<pair<pair<int, int>, int> > Q;
char arr[805][805];
pair<int, int> d[] = {make_pair(1, 0), make_pair(-1, 0), make_pair(0, 1),
                      make_pair(0, -1)};
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
int main() {
  int h, w, k;
  scanf("%d%d%d", &h, &w, &k);
  pair<int, int> st;
  for (int i = 0; i < h; ++i) scanf("%s", arr[i]);
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (arr[i][j] == 'S') st = make_pair(i, j);
  Q.push(make_pair(st, 0));
  mk[st.first][st.second] = 1;
  int ans = mod;
  while (!Q.empty()) {
    pair<int, int> pt = Q.front().first;
    int dis = Q.front().second;
    Q.pop();
    int fl = 0;
    for (int i = 0; i < 4; ++i) {
      pair<int, int> nx = pt + d[i];
      int nd = dis + 1;
      if (arr[nx.first][nx.second] == '#') {
        if (nd <= k) continue;
      }
      if (nx.first * nx.second == 0 || nx.first == h - 1 ||
          nx.second == w - 1) {
        ans = min(ans, nd);
      } else if (mk[nx.first][nx.second] == 0) {
        mk[nx.first][nx.second] = 1;
        Q.push(make_pair(nx, nd));
      }
    }
  }
  if (ans != mod) ans = (ans + k - 1) / k;
  ans = min(ans, 1 + (min(min(st.first, st.second),
                          min(h - 1 - st.first, w - 1 - st.second)) +
                      k - 1) /
                         k);
  printf("%d\n", ans);
  return 0;
}
