#include <bits/stdc++.h>
using namespace std;
long long INF = 1e15;
vector<string> area;
long long H, W, K;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, 1, 0, -1};
long long bfs(long long sy, long long sx) {
  priority_queue<tuple<long long, long long, long long>> que;
  que.push(tuple<long long, long long, long long>(-K, sy, sx));
  vector<vector<long long>> visited(H + 2, vector<long long>(W + 2, 1));
  long long ans =
      min(min(abs(sy - 1), abs(sy - H)), min(abs(sx - 1), abs(sx - W)));
  while (!que.empty()) {
    long long y, x, k;
    tie(k, y, x) = que.top();
    if (visited[y][x] == 0) {
      que.pop();
      continue;
    }
    visited[y][x] = 0;
    k = -k;
    que.pop();
    if (k == 0) continue;
    for (int i = 0; i < 4; i++) {
      if (area[y + dy[i]][x + dx[i]] != '#') {
        que.push(tuple<long long, long long, long long>(-(k - 1), y + dy[i],
                                                        x + dx[i]));
        ans = min(ans, min(min(abs(y + dy[i] - 1), abs(y + dy[i] - H)),
                           min(abs(x + dx[i] - 1), abs(x + dx[i] - W))));
      }
    }
  }
  return ans;
}
int main() {
  cin >> H >> W >> K;
  area.assign(H + 2, string(W + 2, '#'));
  for (int i = 1; i <= H; i++) {
    cin >> area[i];
    area[i] = "#" + area[i] + "#";
  }
  for (int y = 1; y <= H; y++)
    for (int x = 1; x <= W; x++)
      if (area[y][x] == 'S') {
        long long ret = bfs(y, x);
        if (y == 1 || y == H || x == 1 || x == W)
          cout << 0 << endl;
        else if (ret == 0)
          cout << 1 << endl;
        else
          cout << 1 + (ret + K - 1) / K << endl;
        return 0;
        break;
      }
  return 0;
}
