#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e15;
const long long int MOD = 1e9 + 7;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long int h, w, k;
  cin >> h >> w >> k;
  vector<vector<bool>> a(h, vector<bool>(w, false));
  pair<long long int, long long int> s;
  for (long long int i = 0; i < (h); ++i) {
    for (long long int j = 0; j < (w); ++j) {
      char c;
      cin >> c;
      if (c == 'S') {
        s = {i, j};
        a[i][j] = true;
      }
      if (c == '.') {
        a[i][j] = true;
      }
    }
  }
  vector<vector<pair<long long int, long long int>>> d(
      h, vector<pair<long long int, long long int>>(w, {INF, INF}));
  vector<long long int> dir = {1, 0, -1, 0};
  queue<pair<pair<long long int, long long int>,
             pair<long long int, long long int>>>
      q;
  q.push({s, {0, 0}});
  while (not q.empty()) {
    long long int x = q.front().first.first;
    long long int y = q.front().first.second;
    pair<long long int, long long int> dist = q.front().second;
    q.pop();
    if (d[x][y] != make_pair(INF, INF)) {
      continue;
    }
    d[x][y] = dist;
    for (long long int i = 0; i < (4); ++i) {
      long long int nx = x + dir[i];
      long long int ny = y + dir[i ^ 1];
      if (0 > nx || nx >= h || 0 > ny || ny >= w) {
        continue;
      }
      pair<long long int, long long int> nd;
      if (not a[nx][ny]) {
        nd = {dist.first + 1, dist.second + 1};
      } else {
        nd = {dist.first + 1, dist.second};
      }
      if (d[nx][ny] != make_pair(INF, INF)) {
        continue;
      }
      q.push({{nx, ny}, nd});
    }
  }
  long long int ans = INF;
  for (long long int i = 0; i < (h); ++i) {
    for (long long int j = 0; j < (w); ++j) {
      if (i != 0 && i != h - 1 && j != 0 && j != w - 1) {
        continue;
      }
      long long int mv = (d[i][j].first + k - 1) / k;
      long long int open = (d[i][j].second + k - 1) / k;
      if (mv == open) {
        mv++;
      }
      ans = min(ans, mv);
    }
  }
  cout << ans << endl;
  return 0;
}
