#include <bits/stdc++.h>
using namespace std;
int dir[4][2] = {0, -1, 0, 1, -1, 0, 1, 0};
int R, C, K, sx, sy;
vector<string> rooms;
vector<vector<bool> > visited;
void dfs(int x, int y, int remain, vector<int>& vx, vector<int>& vy) {
  if (visited[x][y]) return;
  vx.push_back(x);
  vy.push_back(y);
  visited[x][y] = true;
  if (remain == 0) return;
  for (int k = 0; k < 4; ++k) {
    int xx = x + dir[k][0], yy = y + dir[k][1];
    if (xx < 0 || xx >= R || yy < 0 || yy >= C || rooms[xx][yy] == '#' ||
        visited[xx][yy])
      continue;
    dfs(xx, yy, remain - 1, vx, vy);
  }
}
int check(int x, int y) {
  vector<int> v(4);
  v[0] = x - 0, v[1] = R - 1 - x, v[2] = y - 0, v[3] = C - 1 - y;
  int ret = INT_MAX;
  for (int i = 0; i < 4; ++i) {
    int tmp = v[i] / K;
    if (v[i] % K != 0) {
      ++tmp;
    }
    ret = min(ret, tmp);
  }
  return ret;
}
int main() {
  while (cin >> R >> C >> K) {
    rooms.clear();
    rooms.resize(R);
    visited.clear();
    visited.resize(R, vector<bool>(C, false));
    for (int i = 0; i < R; ++i) {
      cin >> rooms[i];
      for (int j = 0; j < C; ++j) {
        if (rooms[i][j] == 'S') {
          sx = i;
          sy = j;
        }
      }
    }
    if (check(sx, sy) == 0) {
      cout << 0 << endl;
      continue;
    }
    vector<int> vx, vy;
    dfs(sx, sy, K, vx, vy);
    int ans = INT_MAX;
    for (int i = 0; i < vx.size(); ++i) {
      ans = min(ans, check(vx[i], vy[i]));
    }
    cout << ans + 1 << endl;
  }
  return 0;
}
