#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int d[] = {1, 0, -1, 0};
long long h, w, k;
vector<string> a;
vector<vector<int>> dist;
queue<P> qu;
bool isvalid(const P &p) {
  return p.first >= 0 && p.first < h && p.second >= 0 && p.second < w;
}
long long solve();
int main() {
  cin >> h >> w >> k;
  a.resize(h);
  for (int i = 0; i < h; ++i) cin >> a[i];
  cout << solve() << endl;
  return 0;
}
long long solve() {
  dist.assign(h, vector<int>(w, (int)1e9));
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (a[i][j] == 'S') {
        a[i][j] = '.';
        qu.push(P(i, j));
        dist[i][j] = 0;
      }
  while (qu.size()) {
    auto [x, y] = qu.front();
    qu.pop();
    for (int i = 0; i < 4; ++i) {
      int tx = x + d[i], ty = y + d[1 ^ i];
      if (!isvalid(P(tx, ty)) || a[tx][ty] == '#') continue;
      if (dist[tx][ty] > dist[x][y] + 1 && dist[x][y] + 1 <= k) {
        dist[tx][ty] = dist[x][y] + 1;
        qu.push(P(tx, ty));
      }
    }
  }
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (dist[i][j] <= k) {
        dist[i][j] = 0;
        qu.push(P(i, j));
      }
  while (qu.size()) {
    auto [x, y] = qu.front();
    qu.pop();
    for (int i = 0; i < 4; ++i) {
      int tx = x + d[i], ty = y + d[1 ^ i];
      if (!isvalid(P(tx, ty))) continue;
      if (dist[tx][ty] > dist[x][y] + 1) {
        dist[tx][ty] = dist[x][y] + 1;
        qu.push(P(tx, ty));
      }
    }
  }
  int res = 1e9;
  for (int i = 0; i < h; ++i) res = min({res, dist[i][0], dist[i][w - 1]});
  for (int i = 0; i < w; ++i) res = min({res, dist[0][i], dist[h - 1][i]});
  return (res + k - 1) / k + 1;
}
