#include <bits/stdc++.h>
using namespace std;
pair<int, int> getStart(const vector<string> &table) {
  for (int i = 0; i < table.size(); i++) {
    for (int j = 0; j < table[i].length(); j++) {
      if (table[i][j] == 'S') {
        return make_pair(i, j);
      }
    }
  }
  return make_pair(-1, -1);
}
int dist(int sy, int sx, int H, int W) {
  int ans = 10000;
  ans = min(ans, sx);
  ans = min(ans, W - 1 - sx);
  ans = min(ans, sy);
  ans = min(ans, H - 1 - sy);
  return ans;
}
array<array<bool, 801>, 801> visited;
tuple<int, int, int> bfs(const vector<string> &table, const int sy,
                         const int sx, const int K) {
  const int W = table[0].length();
  const int H = table.size();
  int bestY = sy;
  int bestX = sx;
  int bestDist = dist(sy, sx, H, W);
  vector<int> dy = {0, 1, 0, -1};
  vector<int> dx = {1, 0, -1, 0};
  queue<tuple<int, int, int>> q;
  q.push(make_tuple(0, sy, sx));
  while (!q.empty()) {
    int y, x, turn;
    tie(turn, y, x) = q.front();
    visited[y][x] = true;
    int d = dist(y, x, H, W);
    if (d < bestDist) {
      bestDist = d;
      bestY = y;
      bestX = x;
    }
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (turn < K && 0 <= ny && ny < H && 0 <= nx && nx < W &&
          table[ny][nx] != '#') {
        q.push(make_tuple(turn + 1, ny, nx));
      }
    }
  }
  return make_tuple(bestDist, bestY, bestX);
}
int main() {
  for (int i = 0; i <= 800; i++) {
    for (int j = 0; j <= 800; j++) {
      visited[i][j] = false;
    }
  }
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> table(h);
  getline(cin, table[0]);
  for (int i = 0; i < h; i++) {
    getline(cin, table[i]);
  }
  int sy, sx;
  tie(sy, sx) = getStart(table);
  int turn, bestY, bestX;
  tie(turn, bestY, bestX) = bfs(table, sy, sx, k);
  cout << ((turn + k - 1) / k + 1) << endl;
  return 0;
}
