#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int A[808][808];
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, 1, -1};
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  string S[H];
  int sr, sc;
  for (int i = 0; i < (H); i++) {
    cin >> S[i];
    for (int j = 0; j < (W); j++)
      if (S[i][j] == 'S') {
        sr = i;
        sc = j;
      }
  }
  queue<pair<int, pair<int, int> > > que;
  que.push({K, {sr, sc}});
  while (que.size()) {
    pair<int, pair<int, int> > P = que.front();
    que.pop();
    int d = P.first, r = P.second.first, c = P.second.second;
    A[r][c] = 1;
    if (d == 0) continue;
    for (int i = 0; i < (4); i++) {
      int rr = r + dr[i], cc = c + dc[i];
      if (rr >= H || rr < 0 || cc >= W || cc < 0) continue;
      if (S[rr][cc] != '.' || A[rr][cc] != 0) continue;
      que.push({d - 1, {rr, cc}});
    }
  }
  int ans = 1e9;
  for (int i = 0; i < (H); i++)
    for (int j = 0; j < (W); j++)
      if (A[i][j] != 0) {
        int x = i / K + (i % K == 0 ? 0 : 1);
        int y = (H - 1 - i) / K + ((H - 1 - i) % K == 0 ? 0 : 1);
        int z = j / K + (j % K == 0 ? 0 : 1);
        int w = (W - 1 - j) / K + ((W - 1 - j) % K == 0 ? 0 : 1);
        ans = min({ans, x, y, z, w});
      }
  cout << ans + 1 << endl;
  return 0;
}
