#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 20;
const double eps = 1e-8;
char mp[1000][1000];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int vis[1000][1000];
int n, m, k;
int bfs(int sx, int sy) {
  memset((vis), 0, sizeof(vis));
  int ans = INF;
  queue<pair<int, int> > q;
  q.push(make_pair((sx), (sy)));
  while (!q.empty()) {
    pair<int, int> nw = q.front();
    q.pop();
    int sp =
        min(min(nw.first - 1, nw.second - 1), min(n - nw.first, m - nw.second));
    ans = min(sp / k + ((sp % k) ? 1 : 0), ans);
    for (int i = 0; i < 4; i++) {
      int nx = nw.first + dir[i][0];
      int ny = nw.second + dir[i][1];
      if (nx > 0 && ny > 0 && nx <= n && ny <= m && mp[nx][ny] == '.') {
        if (!vis[nx][ny]) vis[nx][ny] = 1, q.push(make_pair((nx), (ny)));
      }
    }
  }
  return ans;
}
int main() {
  while (cin >> n >> m >> k) {
    getchar();
    int sx = -1, sy = -1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%c", &mp[i][j]);
        if (mp[i][j] == 'S') sx = i, sy = j;
      }
      getchar();
    }
    int ans = bfs(sx, sy);
    printf("%d\n", ans + 1);
  }
  return 0;
}
