#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
vector<vector<int>> G;
int main(int argc, char const *argv[]) {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> ss(h);
  for (int i = 0; i < h; ++i) cin >> ss[i];
  int s, t;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < h; ++j) {
      if (ss[i][j] == 'S') {
        s = i;
        t = j;
      }
    }
  }
  vector<vector<int>> dist(h, vector<int>(w, -1));
  queue<pair<int, int>> que;
  que.push(pair<int, int>(s, t));
  int res = 1 << 30;
  dist[s][t] = 0;
  res = min(res, s);
  res = min(res, h - 1 - s);
  res = min(res, t);
  res = min(res, w - 1 - t);
  while (!que.empty()) {
    pair<int, int> u = que.front();
    que.pop();
    res = min(res, u.first);
    res = min(res, h - 1 - u.first);
    res = min(res, u.second);
    res = min(res, w - 1 - u.second);
    vector<pair<int, int>> tmp;
    if (u.first > 0) tmp.push_back(pair<int, int>(u.first - 1, u.second));
    if (u.second > 0) tmp.push_back(pair<int, int>(u.first, u.second - 1));
    if (u.first < h - 1) tmp.push_back(pair<int, int>(u.first + 1, u.second));
    if (u.second < w - 1) tmp.push_back(pair<int, int>(u.first, u.second + 1));
    for (pair<int, int> v : tmp) {
      if (dist[v.first][v.second] == -1 && ss[v.first][v.second] == '.') {
        dist[v.first][v.second] = dist[u.first][u.second] + 1;
        if (dist[v.first][v.second] <= k) que.push(v);
      }
    }
  }
  cout << (1 + (res + k - 1) / k) << endl;
  return 0;
}
