#include <bits/stdc++.h>
using namespace std;
const int inf = 1001001001;
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
int h, w, k;
char c[1000][1000];
int d[1000][1000];
bool inside(int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; }
int main() {
  cin >> h >> w >> k;
  for (int i = 0; i < 1000; ++i) {
    for (int j = 0; j < 1000; ++j) {
      d[i][j] = inf;
    }
  }
  queue<pair<int, int>> q;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> c[i][j];
      if (c[i][j] == 'S') {
        q.push(pair<int, int>(i, j));
        d[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    int y = q.front().first;
    int x = q.front().second;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (inside(ny, nx) && c[ny][nx] == '.' && d[ny][nx] > d[y][x] + 1) {
        d[ny][nx] = d[y][x] + 1;
        if (d[ny][nx] != k) {
          q.push(make_pair(ny, nx));
        }
      }
    }
  }
  int ans = inf;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (d[i][j] == inf) continue;
      int dist = min({i, j, h - i, w - j});
      ans = min(ans, (dist + k - 1) / k + 1);
    }
  }
  cout << ans << endl;
}
