#include <bits/stdc++.h>
using namespace std;
long long w, h, k, dis[808][808];
pair<long long, long long> d[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool mark[808][808];
char a[808][808];
queue<pair<long long, long long> > q;
bool isval(long long x, long long y) {
  return (x >= 0 && x < h && y >= 0 && y < w && !mark[x][y] && a[x][y] != '#');
}
void bfs() {
  while ((long long)q.size()) {
    auto v = q.front();
    q.pop();
    for (long long i = 0; i < 4; i++) {
      long long x = v.first + d[i].first, y = v.second + d[i].second;
      if (isval(x, y)) {
        dis[x][y] = dis[v.first][v.second] + 1;
        q.push({x, y});
        mark[x][y] = 1;
      }
    }
  }
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> w >> h >> k;
  long long xs, ys;
  for (long long i = 0; i < w; i++)
    for (long long j = 0; j < h; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') xs = i, ys = j;
    }
  memset(dis, 63, sizeof dis);
  dis[xs][ys] = 0;
  mark[xs][ys] = 1;
  q.push({xs, ys});
  bfs();
  long long mini, ans = INT_MAX;
  for (long long i = 0; i < w; i++)
    for (long long j = 0; j < h; j++)
      if (dis[i][j] <= k) {
        mini = min(min(i, j), min(w - i - 1, h - j - 1));
        ans = min(ans, 1 + (mini + k - 1) / k);
      }
  cout << ans << endl;
  return 0;
}
