#include <bits/stdc++.h>
using namespace std;
const int MAXH = 800 + 3;
unsigned long long operator""_D(unsigned long long x) { return 4; }
int h, w, k, dis[MAXH][MAXH], ans = INT_MAX;
pair<int, int> st, d[] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
string s[MAXH];
queue<pair<int, int> > q;
bitset<MAXH> mark[MAXH];
inline bool in(pair<int, int> x) {
  return x.first < h && x.second < w && ~x.first && ~x.second;
}
void bfs(pair<int, int> v) {
  dis[v.first][v.second] = 0;
  mark[v.first][v.second] = true;
  q.push(v);
  while (!q.empty()) {
    pair<int, int> x = q.front();
    q.pop();
    for (int i = 0; i < 0_D; i++)
      if (in({x.first + d[i].first, x.second + d[i].second}) &&
          s[x.first + d[i].first][x.second + d[i].second] != '#' &&
          !mark[x.first + d[i].first][x.second + d[i].second]) {
        q.push({x.first + d[i].first, x.second + d[i].second});
        mark[x.first + d[i].first][x.second + d[i].second] = true;
        dis[x.first + d[i].first][x.second + d[i].second] =
            dis[x.second][x.second] + 1;
      }
  }
}
int main() {
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
    for (int j = 0; j < w; j++)
      if (s[i][j] == 'S') st = {i, j};
  }
  memset(dis, 127, sizeof dis);
  bfs(st);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (dis[i][j] <= k)
        ans = min({ans, (i + k - 1) / k + 1, (h - i - 1 + k - 1) / k + 1,
                   (j + k - 1) / k + 1, (w - j - 1 + k - 1) / k + 1});
  cout << ans;
  return 0;
}
