#include <bits/stdc++.h>
const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
long long max(long long a, int b) { return max(a, long long(b)); }
long long max(int a, long long b) { return max(long long(a), b); }
long long min(long long a, int b) { return min(a, long long(b)); }
long long min(int a, long long b) { return min(long long(a), b); }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> v(h);
  for (long long i = 0; i < h; ++i) cin >> v[i];
  int sx, sy;
  for (long long i = 0; i < h; ++i) {
    for (long long j = 0; j < w; ++j) {
      if (v[i][j] == 'S') {
        sy = i;
        sx = j;
      }
    }
  }
  vector<vector<int> > depth(h, vector<int>(w, 1000000001ll));
  queue<pair<int, int> > q;
  q.push({sx, sy});
  depth[sy][sx] = 0;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if (x != 0 && depth[y][x - 1] == 1000000001ll && v[y][x - 1] != '#') {
      q.push({x - 1, y});
      depth[y][x - 1] = depth[y][x] + 1;
    }
    if (y != 0 && depth[y - 1][x] == 1000000001ll && v[y - 1][x] != '#') {
      q.push({x, y - 1});
      depth[y - 1][x] = depth[y][x] + 1;
    }
    if (x != w - 1 && depth[y][x + 1] == 1000000001ll && v[y][x + 1] != '#') {
      q.push({x + 1, y});
      depth[y][x + 1] = depth[y][x] + 1;
    }
    if (y != h - 1 && depth[y + 1][x] == 1000000001ll && v[y + 1][x] != '#') {
      q.push({x, y + 1});
      depth[y + 1][x] = depth[x][y] + 1;
    }
  }
  int dist = 1000000001ll;
  for (long long i = 0; i < h; ++i) {
    for (long long j = 0; j < w; ++j) {
      if (depth[i][j] <= k) {
        dist = min(dist, min(i, min(j, min(h - i - 1, w - j - 1))));
      }
    }
  }
  cout << (dist + k - 1) / k + 1 << endl;
  return 0;
}
