#include <bits/stdc++.h>
using namespace std;
vector<pair<int, pair<int, int>>> G[1000][1000];
int dist[1000][1000];
bool used[1000][1000];
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
               greater<pair<int, pair<int, int>>>>
    que;
void dijkstra(pair<int, int> s) {
  que.push({0, s});
  while (!que.empty()) {
    pair<int, pair<int, int>> p = que.top();
    que.pop();
    int d = p.first, posx = p.second.first, posy = p.second.second;
    if (used[posx][posy]) continue;
    used[posx][posy] = true;
    dist[posx][posy] = d;
    for (auto&& i : G[posx][posy]) {
      int x = i.second.first, y = i.second.second;
      if (dist[x][y] > d + 1) {
        que.push({d + 1, make_pair(x, y)});
      }
    }
  }
}
int main() {
  int i, j, h, w, k;
  string s[1000];
  pair<int, int> p;
  cin >> h >> w >> k;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      used[i][j] = false;
    }
  }
  for (i = 0; i < h; i++) {
    cin >> s[i];
    for (j = 0; j < w; j++) {
      dist[i][j] = 10000000;
      if (s[i][j] == 'S') {
        p = {i, j};
      }
      if (s[i][j] == '#') continue;
      if (i != 0 && s[i - 1][j] != '#') {
        G[i][j].push_back({1, make_pair(i - 1, j)});
        G[i - 1][j].push_back({1, make_pair(i, j)});
      }
      if (j != 0 && s[i][j - 1] != '#') {
        G[i][j].push_back({1, make_pair(i, j - 1)});
        G[i][j - 1].push_back({1, make_pair(i, j)});
      }
    }
  }
  dijkstra(p);
  int ans = 1000;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      int x = min(min(i, h - 1 - i), min(j, w - 1 - j));
      if (dist[i][j] <= k) {
        if (x == 0)
          ans = 0;
        else
          ans = min(ans, x / (min(k, x)));
      }
    }
  }
  cout << ans + 1 << endl;
}
