#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
struct Pack {
  int first, second, cost;
};
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const int N = 805;
int dist[N][N];
char mx[N][N];
bool f[N][N];
int n, m, k, sx, sy, ant;
static bool ok(pii nod) {
  return (1 <= nod.first && nod.first <= n) &&
         (1 <= nod.second && nod.second <= m);
}
static void bfs() {
  deque<pii> dq;
  pii u, v;
  dist[sx][sy] = 1;
  dq.push_back({sx, sy});
  while (!dq.empty()) {
    u = dq.front(), dq.pop_front();
    if (dist[v.first][v.second] <= k)
      for (int dir = 0; dir < 4; ++dir) {
        v.first = u.first + dx[dir];
        v.second = u.second + dy[dir];
        if (!ok(v) || dist[v.first][v.second] || mx[v.first][v.second] == '#')
          continue;
        f[v.first][v.second] = 1;
        dist[v.first][v.second] = dist[u.first][u.second] + 1;
        dq.push_back(v);
      }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> (mx[i] + 1);
    for (int j = 1; j <= m; ++j)
      if (mx[i][j] == 'S') sx = i, sy = j;
  }
  bfs();
  ant = 2e9;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (dist[i][j]) {
        ant = min(ant, (min(i - 1, n - i) + k - 1) / k + 1);
        ant = min(ant, (min(j - 1, m - j) + k - 1) / k + 1);
      }
  cout << ant << endl;
  return 0;
}
