#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
const int INF = 1 << 29;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int H, W, K;
const int MAX_HW = 810;
const int MAX_K = MAX_HW * MAX_HW;
char board[MAX_HW][MAX_HW];
bool visited[MAX_HW][MAX_HW];
int sy, sx;
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> board[i][j];
      if (board[i][j] == 'S') {
        sy = i;
        sx = j;
      }
    }
  }
  queue<pair<pii, int>> que;
  que.emplace(make_pair(make_pair(sy, sx), 0));
  while (not que.empty()) {
    auto p = que.front();
    que.pop();
    auto pos = p.first;
    int cnt = p.second;
    if (cnt > K) {
      break;
    }
    int py, px;
    py = pos.first;
    px = pos.second;
    if (py == 0 or py == H - 1 or px == 0 or px == W - 1) {
      cout << 1 << endl;
      return 0;
    }
    if (visited[py][px]) continue;
    visited[py][px] = true;
    for (int i = 0; i < 4; i++) {
      int ny = py + dy[i];
      int nx = px + dx[i];
      if (visited[ny][nx]) continue;
      que.emplace(make_pair(make_pair(ny, nx), cnt + 1));
    }
  }
  int result = 1 << 30;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (visited[i][j]) {
        int c1, c2, c3, c4;
        c1 = abs(i - 0);
        c2 = abs(i - (H - 1));
        c3 = abs(j - 0);
        c4 = abs(j - (W - 1));
        result = min(result, c1);
        result = min(result, c2);
        result = min(result, c3);
        result = min(result, c4);
      }
    }
  }
  cout << (result + (K - 1)) / K + 1 << endl;
  return 0;
}
