#include <bits/stdc++.h>
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}
long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
int main() {
  int h = in(), w = in(), k = in(), sh = -1, sw = -1;
  vector<vector<int>> a(h, vector<int>(w));
  for (int i = 0; i < h; i++) {
    string s = stin();
    for (int j = 0; j < w; j++) {
      a[i][j] = s[j] == '#' ? 1 : 0;
      if (s[j] == 'S') {
        sh = i;
        sw = j;
      }
    }
  }
  vector<vector<bool>> done(h, vector<bool>(w, false));
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      q;
  q.push(make_pair(0, make_pair(sh, sw)));
  int min_cost = 200000;
  while (!q.empty()) {
    int s = q.top().second.first, t = q.top().second.second,
        cost = q.top().first;
    q.pop();
    done[s][t] = true;
    if (s == 0 || s == h - 1 || t == 0 || t == w - 1) {
      min_cost = cost;
      break;
    }
    if (!done[s + 1][t]) {
      if (a[s + 1][t] == 0)
        q.push(make_pair(cost, make_pair(s + 1, t)));
      else
        q.push(make_pair(cost + 1, make_pair(s + 1, t)));
    }
    if (!done[s - 1][t]) {
      if (a[s - 1][t] == 0)
        q.push(make_pair(cost, make_pair(s - 1, t)));
      else
        q.push(make_pair(cost + 1, make_pair(s - 1, t)));
    }
    if (!done[s][t + 1]) {
      if (a[s][t + 1] == 0)
        q.push(make_pair(cost, make_pair(s, t + 1)));
      else
        q.push(make_pair(cost + 1, make_pair(s, t + 1)));
    }
    if (!done[s][t - 1]) {
      if (a[s][t - 1] == 0)
        q.push(make_pair(cost, make_pair(s, t - 1)));
      else
        q.push(make_pair(cost + 1, make_pair(s, t - 1)));
    }
  }
  cout << (min_cost + k - 1) / k + 1 << endl;
}
