#include <bits/stdc++.h>
using namespace std;
long long pwr(long long base, long long p, long long mod) {
  long long ans = 1;
  while (p) {
    if (p & 1) ans = (ans * base) % mod;
    base = (base * base) % mod;
    p /= 2;
  }
  return ans;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int r, c, k, dist[805][805];
char arr[805][805];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int x, y;
  cin >> r >> c >> k;
  for (int i = 1; i <= r; i++) {
    cin >> (arr[i] + 1);
    for (int j = 1; j <= c; j++) {
      if (arr[i][j] == 'S') {
        x = i;
        y = j;
      }
      dist[i][j] = (1000000007LL);
    }
  }
  priority_queue<pair<int, pair<int, int> > > pq;
  dist[x][y] = 0;
  pq.push(pair<int, pair<int, int> >(0, pair<int, int>(x, y)));
  while (!pq.empty()) {
    int x = pq.top().second.first, y = pq.top().second.second;
    int d = -pq.top().first;
    pq.pop();
    if (d > dist[x][y]) continue;
    for (int i = x - 1; i <= x + 1; i++)
      for (int j = y - 1; j <= y + 1; j++)
        if (abs(i - x) + abs(j - y) == 1 && i >= 1 && i <= r && j >= 1 &&
            j <= c && arr[i][j] != '#') {
          int dd = d + 1;
          if (dd < dist[i][j] && dd <= k) {
            dist[i][j] = dd;
            pq.push(pair<int, pair<int, int> >(-dd, pair<int, int>(i, j)));
          }
        }
  }
  int ans = (1000000007LL);
  for (int i = 1; i <= r; i++) ans = min(ans, min(dist[i][1], dist[i][c]));
  for (int j = 1; j <= c; j++) ans = min(ans, min(dist[1][j], dist[r][j]));
  if (ans != (1000000007LL)) {
    ans = (ans + k - 1) / k;
    cout << ans;
    return 0;
  }
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++)
      if (dist[i][j] != (1000000007LL)) {
        pq.push(pair<int, pair<int, int> >(-dist[i][j], pair<int, int>(i, j)));
      }
  while (!pq.empty()) {
    int x = pq.top().second.first, y = pq.top().second.second;
    int d = -pq.top().first;
    pq.pop();
    if (d > dist[x][y]) continue;
    for (int i = x - 1; i <= x + 1; i++)
      for (int j = y - 1; j <= y + 1; j++)
        if (abs(i - x) + abs(j - y) == 1 && i >= 1 && i <= r && j >= 1 &&
            j <= c) {
          int dd = d + 1;
          if (dd < dist[i][j]) {
            dist[i][j] = dd;
            pq.push(pair<int, pair<int, int> >(-dd, pair<int, int>(i, j)));
          }
        }
  }
  for (int i = 1; i <= r; i++) ans = min(ans, min(dist[i][1], dist[i][c]));
  for (int j = 1; j <= c; j++) ans = min(ans, min(dist[1][j], dist[r][j]));
  cout << (ans + k - 1) / k;
  return 0;
}
