#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 1LL << 61;
vector<long long> idy = {0, 1, 0, -1};
vector<long long> idx = {1, 0, -1, 0};
int main() {
  long long h, w, k;
  cin >> h >> w >> k;
  vector<string> maze(h);
  for (long long i = 0; i < h; i++) cin >> maze[i];
  vector<vector<long long>> d(h, vector<long long>(w, -1));
  priority_queue<pair<long long, pair<long long, long long>>,
                 vector<pair<long long, pair<long long, long long>>>,
                 greater<pair<long long, pair<long long, long long>>>>
      q;
  long long sy, sx;
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++)
      if (maze[i][j] == 'S') sy = i, sx = j;
  q.push({0, {sy, sx}});
  while (q.size()) {
    long long fy = q.top().second.first;
    long long fx = q.top().second.second;
    long long fd = q.top().first;
    q.pop();
    if (d[fy][fx] > -1) continue;
    d[fy][fx] = fd;
    for (long long i = 0; i < 4; i++) {
      long long ny = fy + idy[i];
      long long nx = fx + idx[i];
      long long nd = fd + 1;
      if (ny > -1 && nx > -1 && ny < h && nx < w && nd <= k &&
          maze[ny][nx] != '#') {
        q.push({nd, {ny, nx}});
      }
    }
  }
  for (long long i = 0; i < h; i++) {
    if (d[i][0] > -1 || d[i][w - 1] > -1) {
      cout << 1 << endl;
      return 0;
    }
  }
  for (long long i = 0; i < h; i++) {
    if (d[0][i] > -1 || d[h - 1][0] > -1) {
      cout << 1 << endl;
      return 0;
    }
  }
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++)
      if (d[i][j] > -1) d[i][j] = -1, q.push({0, {i, j}});
  while (q.size()) {
    long long fy = q.top().second.first;
    long long fx = q.top().second.second;
    long long fd = q.top().first;
    q.pop();
    if (d[fy][fx] > -1) continue;
    d[fy][fx] = fd;
    for (long long i = 0; i < 4; i++) {
      long long ny = fy + idy[i];
      long long nx = fx + idx[i];
      long long nd = fd + 1;
      if (ny > -1 && nx > -1 && ny < h && nx < w) {
        q.push({nd, {ny, nx}});
      }
    }
  }
  long long mi = inf;
  for (long long i = 0; i < h; i++) {
    mi = min({mi, d[i][0], d[i][w - 1]});
  }
  for (long long i = 0; i < w; i++) {
    mi = min({mi, d[0][i], d[h - 1][i]});
  }
  cout << (mi + k - 1) / k + 1 << endl;
}
