#include <bits/stdc++.h>
using namespace std;
class Dinic {
 public:
  vector<vector<pair<long long, long long>>> v;
  vector<long long> e;
  vector<long long> k;
  vector<long long> j;
  void init(long long n) {
    v = vector<vector<pair<long long, long long>>>(n);
    e = vector<long long>();
    k = vector<long long>(n);
    j = vector<long long>(n);
  }
  void add_edge(long long a, long long b, long long atob, long long btoa = 0) {
    v[a].push_back(make_pair(b, e.size()));
    e.push_back(atob);
    v[b].push_back(make_pair(a, e.size()));
    e.push_back(btoa);
  }
  void dyk(long long s) {
    for (auto& i : k) {
      i = 1000000007LL;
    }
    queue<pair<long long, long long>> pq;
    pq.push(make_pair(0, s));
    while (!pq.empty()) {
      long long w = pq.front().first;
      long long b = pq.front().second;
      pq.pop();
      if (k[b] > w) {
        k[b] = w;
        for (auto i : v[b]) {
          if (e[i.second] > 0) {
            pq.push(make_pair(w + 1, i.first));
          }
        }
      }
    }
  }
  long long dfs(long long s, long long t, long long p, long long c) {
    if (p == t) {
      return c;
    }
    long long all = 0;
    while (c > all && j[p] < v[p].size()) {
      if (k[v[p][j[p]].first] == k[p] + 1) {
        long long r =
            dfs(s, t, v[p][j[p]].first, min(c - all, e[v[p][j[p]].second]));
        e[v[p][j[p]].second] -= r;
        e[v[p][j[p]].second ^ 1] += r;
        if (r < c - all) {
          j[p]++;
        }
        all += r;
      } else {
        j[p]++;
      }
    }
    return all;
  }
  long long solve(long long s, long long t) {
    long long all = 0;
    while (1) {
      dyk(s);
      for (auto& i : j) {
        i = 0;
      }
      long long r = dfs(s, t, s, 1000000007LL);
      if (r == 0) {
        return all;
      }
      all += r;
    }
  }
};
class Rmq {
 public:
  vector<long long> d;
  long long n;
  void init(long long nw) {
    n = 1;
    while (n < nw) {
      n *= 2;
    }
    d.resize(n * 2);
    for (long long i = 0; i < (n * 2); i++) {
      d[i] = 1000000007LL * 1000000007LL;
    }
  }
  void set_(long long b, long long x, long long p, long long nb, long long ne) {
    if (nb <= b && b < ne) {
      if (nb == ne - 1) {
        d[p] = min(d[p], x);
      } else {
        long long m = (nb + ne) / 2;
        set_(b, x, p * 2 + 1, nb, m);
        set_(b, x, p * 2 + 2, m, ne);
        d[p] = min(d[p * 2 + 1], d[p * 2 + 2]);
      }
    }
  }
  void set(long long b, long long x) { set_(b, x, 0, 0, n); }
  long long get_(long long b, long long e, long long p, long long nb,
                 long long ne) {
    if (b <= nb && ne <= e) {
      return d[p];
    } else if (ne <= b || e <= nb) {
      return 1000000007LL * 1000000007LL;
    } else {
      long long m = (nb + ne) / 2;
      return min(get_(b, e, p * 2 + 1, nb, m), get_(b, e, p * 2 + 2, m, ne));
    }
  }
  long long get(long long b, long long e) { return get_(b, e, 0, 0, n); }
};
string s[1919];
long long d[1919][1919];
long long dy[4] = {0, 1, 0, -1};
long long dx[4] = {1, 0, -1, 0};
signed main() {
  long long h, w, k;
  cin >> h >> w >> k;
  for (long long i = 0; i < (h); i++) {
    cin >> s[i];
  }
  queue<pair<long long, pair<long long, long long>>> pq;
  for (long long i = 0; i < (h); i++) {
    for (long long j = 0; j < (w); j++) {
      if (s[i][j] == 'S') {
        pq.push(make_pair(0, make_pair(i, j)));
      }
      d[i][j] = 1000000007LL;
    }
  }
  long long ans = 1000000007LL;
  while (!pq.empty()) {
    long long a = pq.front().first;
    pair<long long, long long> b = pq.front().second;
    pq.pop();
    if (0 <= b.first && b.first < h && 0 <= b.second && b.second < w &&
        s[b.first][b.second] != '#' && d[b.first][b.second] > a && a <= k) {
      d[b.first][b.second] = a;
      ans = min(
          ans,
          (min(min(b.first, h - 1 - b.first), min(b.second, w - b.second)) + k -
           1) / k +
              1);
      for (long long i = 0; i < (4); i++) {
        pq.push(make_pair(a + 1, make_pair(b.first + dy[i], b.second + dx[i])));
      }
    }
  }
  cout << ans << endl;
}
