#include <bits/stdc++.h>
using namespace std;
int h, w, k, flag = 0;
long long res = 0xfffffff;
int si, sj, ni, nj;
int graph[805][805];
int vis[805][805];
int min4(int c1, int c2, int c3, int c4) {
  return min(min(c1, c2), min(c3, c4));
}
long long dis(int i, int j) {
  return ceil((double)min4(i - 1, j - 1, h - i, w - j) / k);
}
void dfs(int i, int j, int s) {
  if (flag) return;
  if (s < 0) return;
  if (graph[i][j] || vis[i][j]) return;
  if (i == 1 || j == 1 || i == h || j == w) {
    flag = 1;
    res = 0;
    return;
  }
  vis[i][j] = 1;
  dfs(i + 1, j, s - 1);
  dfs(i - 1, j, s - 1);
  dfs(i, j + 1, s - 1);
  dfs(i, j - 1, s - 1);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> k;
  char line[w + 5];
  for (int i = 1; i <= h; i++) {
    cin >> line;
    for (int j = 1; j <= w; j++) {
      if (line[j - 1] == '#')
        graph[i][j] = 1;
      else if (line[j - 1] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  queue<pair<pair<int, int>, int> > q;
  q.push(make_pair(make_pair(si, sj), 0));
  while (!q.empty()) {
    pair<pair<int, int>, int> u = q.front();
    q.pop();
    pair<int, int> p = u.first;
    int step = u.second;
    int ni = p.first, nj = p.second;
    vis[ni][nj] = 1;
    if (step > k) break;
    if (ni == 1 || nj == 1 || ni == h || nj == w) {
      res = 0;
      break;
    }
    res = min(res, dis(ni, nj));
    for (int i = ni - 1; i <= ni + 1; i++) {
      for (int j = nj - 1; j <= nj + 1; j++) {
        if (vis[i][j]) continue;
        if (graph[i][j]) continue;
        q.push(make_pair(make_pair(i, j), step + 1));
      }
    }
  }
  cout << res + 1 << endl;
  return 0;
}
