#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T re() {
  T N = 0;
  char c = getchar();
  bool neg = 0;
  for (; c < '0' || c > '9'; c = getchar()) neg |= c == '-';
  for (; c >= '0' && c <= '9'; c = getchar()) N = (N << 3) + (N << 1) + c - '0';
  return neg ? -N : N;
}
int h, w, k;
inline bool ins(int r, int c) { return 1 <= r && r <= h && 1 <= c && c <= w; }
char g[805][805];
int d[805][805][2];
struct dt {
  int r, c, dm, dk;
  dt(int r = 0, int c = 0, int dm = 0, int dk = 0)
      : r(r), c(c), dm(dm), dk(dk) {}
  bool operator<(const dt &rhs) const {
    return dm == rhs.dm ? dk > rhs.dk : dm > rhs.dm;
  }
};
priority_queue<dt> pq;
int dr[] = {1, 0, -1, 0};
int dc[] = {0, 1, 0, -1};
queue<pair<int, int> > q;
bool vis[805][805];
void bfs(pair<int, int> st) {
  memset(vis, 0, sizeof(vis));
  q.push(st);
  vis[st.first][st.second] = 1;
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    for (int i = 0, r, c; i < 4; i++) {
      r = u.first + dr[i];
      c = u.second + dc[i];
      if (!ins(r, c)) continue;
      if (vis[r][c]) continue;
      d[r][c][0] = d[u.first][u.second][0] + 1;
      d[r][c][1] = d[u.first][u.second][1] + (g[r][c] == '#');
      vis[r][c] = 1;
      q.emplace(r, c);
    }
  }
}
int main() {
  scanf("%d %d %d", &h, &w, &k);
  pair<int, int> st = {-1, -1};
  for (int i = 1; i <= h; i++) {
    scanf("%s", g[i] + 1);
    for (int j = 1; j <= w; j++)
      if (g[i][j] == 'S') {
        st = {i, j};
        g[i][j] = '#';
      }
  }
  bfs(st);
  int mn = 0x3f3f3f3f;
  for (int i = 1; i <= h; i++) {
    mn = min(mn, max((d[i][1][0] + k - 1) / k, 1 + (d[i][1][1] + k - 1) / k));
    mn = min(mn, max((d[i][w][0] + k - 1) / k, 1 + (d[i][w][1] + k - 1) / k));
  }
  for (int i = 1; i <= w; i++) {
    mn = min(mn, max((d[1][i][0] + k - 1) / k, 1 + (d[1][i][1] + k - 1) / k));
    mn = min(mn, max((d[h][i][0] + k - 1) / k, 1 + (d[h][i][1] + k - 1) / k));
  }
  cout << mn << '\n';
  return 0;
}
