#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  x = 0;
  bool flag = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') {
      flag = 1;
    }
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  if (flag) {
    x = -x;
  }
}
const int N = 805;
int n, m, k, sx, sy, ans = 1e9;
char mp[N][N];
bool vis[N][N];
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
void bfs() {
  queue<pair<int, int>> q;
  q.push(make_pair(sx, sy));
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    vis[x][y] = 1;
    ans = min(ans, (min(min(x - 1, n - x), min(y - 1, m - y)) + k - 1) / k + 1);
    for (int i = 0; i < 4; ++i) {
      int X = x + dx[i], Y = y + dy[i];
      if (X >= 1 && X <= n && Y >= 1 && Y <= m &&
          abs(sx - X) + abs(sy - Y) <= k && !vis[X][Y] && mp[X][Y] != '#') {
        q.push(make_pair(X, Y));
      }
    }
  }
}
int main() {
  read(n), read(m), read(k);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", mp[i] + 1);
    for (int j = 1; j <= m; ++j) {
      if (mp[i][j] == 'S') {
        sx = i, sy = j;
      }
    }
  }
  bfs();
  printf("%d\n", ans);
  return 0;
}
