#include <bits/stdc++.h>
using namespace std;
const int maxn = 805;
int h, w, k, sx, sy, mn = 1e9;
string s[maxn];
int dist[maxn][maxn];
queue<pair<int, int> > q;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int get_dis(int x, int y) { return min(min(x, h - x - 1), min(y, w - y - 1)); }
bool onboard(int x, int y) { return x >= 0 && x < h && y >= 0 && y < w; }
void bfs() {
  q.push(make_pair(sx, sy));
  for (int(i) = 0; (i) < maxn; i++)
    for (int(j) = 0; (j) < maxn; j++) dist[i][j] = -1;
  dist[sx][sy] = 0;
  mn = 1e9;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    mn = min(mn, get_dis(x, y));
    if (dist[x][y] == k) continue;
    for (int(i) = 0; (i) < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (dist[nx][ny] != -1 || s[nx][ny] == '#' || !onboard(nx, ny)) continue;
      dist[nx][ny] = dist[x][y] + 1;
      q.push(make_pair(nx, ny));
    }
  }
}
int main() {
  cin >> h >> w >> k;
  for (int(i) = 0; (i) < h; i++) {
    cin >> s[i];
    for (int(j) = 0; (j) < w; j++)
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
      }
  }
  bfs();
  cout << mn / k + (mn % k == 0 ? 0 : 1) + 1 << endl;
  return 0;
}
