#include <bits/stdc++.h>
using namespace std;
template <class A>
void pr(A a) {
  cout << a;
  cout << '\n';
}
template <class A, class B>
void pr(A a, B b) {
  cout << a << ' ';
  pr(b);
}
template <class A, class B, class C>
void pr(A a, B b, C c) {
  cout << a << ' ';
  pr(b, c);
}
template <class A, class B, class C, class D>
void pr(A a, B b, C c, D d) {
  cout << a << ' ';
  pr(b, c, d);
}
template <class A>
void PR(A a, long long n) {
  for (int i = (int)(0); i < (int)(n); i++) {
    if (i) cout << ' ';
    cout << a[i];
  }
  cout << '\n';
}
long long check(long long n, long long m, long long x, long long y) {
  return x >= 0 && x < n && y >= 0 && y < m;
}
const long long MAX = 1000000007, MAXL = 1LL << 61, dx[4] = {-1, 0, 1, 0},
                dy[4] = {0, 1, 0, -1};
string s[888];
pair<int, int> d[888][888];
void Main() {
  int h, w, t;
  cin >> h >> w >> t;
  priority_queue<pair<pair<int, int>, pair<int, int> >,
                 vector<pair<pair<int, int>, pair<int, int> > >,
                 greater<pair<pair<int, int>, pair<int, int> > > >
      que;
  for (int i = (int)(0); i < (int)(h); i++) {
    cin >> s[i];
    for (int j = (int)(0); j < (int)(w); j++) {
      if (s[i][j] == 'S') {
        d[i][j] = pair<int, int>(0, 0);
        que.push(pair<pair<int, int>, pair<int, int> >(pair<int, int>(0, 0),
                                                       pair<int, int>(i, j)));
      }
      d[i][j] = pair<int, int>(MAX, MAX);
    }
  }
  int ans = MAX;
  while (!que.empty()) {
    pair<pair<int, int>, pair<int, int> > p = que.top();
    que.pop();
    int xx = p.second.first, yy = p.second.second;
    pair<int, int> q = p.first;
    if (d[xx][yy] < q) continue;
    for (int i = (int)(0); i < (int)(4); i++) {
      int x = xx + dx[i], y = yy + dy[i];
      if (!check(h, w, x, y)) continue;
      pair<int, int> e = q;
      if (s[x][y] == '#')
        e.second += 2;
      else
        e.first++;
      if (e.first < e.second / 2) e.first += t;
      if (!x || !y || x == h - 1 || y == w - 1)
        ans = min(ans, (int)max(ceil((double)(e.first + e.second / 2) / t),
                                ceil((double)e.second / 2 / t)));
      if (d[x][y] > e) {
        d[x][y] = e;
        que.push(
            pair<pair<int, int>, pair<int, int> >(e, pair<int, int>(x, y)));
      }
    }
  }
  pr(ans);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  Main();
  return 0;
}
