#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[4];
queue<pair<pair<int, int>, pair<int, int> > > q;
char m[880][880];
int h, w, k, sr, sc;
bool inma(int r, int c, int i) {
  r += a[i].first;
  c += a[i].second;
  if (r > 0 && r <= h && c > 0 && c <= w && m[r][c] != '#')
    return true;
  else
    return false;
}
int main() {
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= h; j++) {
      cin >> m[i][j];
      if (m[i][j] == 'S') {
        sr = i;
        sc = j;
      }
    }
  }
  if (sr == 1 || sr == h || sc == 1 || sc == w) {
    cout << 0 << endl;
    return 0;
  }
  a[0] = make_pair(1, 0);
  a[1] = make_pair(0, 1);
  a[2] = make_pair(0, -1);
  a[3] = make_pair(-1, 0);
  q.push(make_pair(make_pair(sr, sc), make_pair(0, -1)));
  int ans = min(sr - 1, min(sc - 1, min(h - sr + 1, w - sc + 1)));
  while (!q.empty()) {
    pair<pair<int, int>, pair<int, int> > t;
    t = q.front();
    q.pop();
    if (t.second.first != k) {
      int r = t.first.first;
      int c = t.first.second;
      for (int i = 0; i < 4; i++) {
        if (i != t.second.second && inma(r, c, i)) {
          int r1 = r + a[i].first, c1 = c + a[i].second;
          q.push(make_pair(make_pair(r1, c1),
                           make_pair(t.second.first + 1, 4 - i)));
          ans = min(ans, min(r1 - 1, min(c1 - 1, min(h - r1 + 1, w - c1 + 1))));
        }
      }
    }
  }
  if (ans == 0) {
    cout << 1 << endl;
  } else {
    cout << (ans - 1) / k + 2 << endl;
  }
  return 0;
}
