#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int qx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int qy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int main(void) {
  int H, W, K;
  cin >> H >> W >> K;
  pair<long long, long long> s;
  vector<string> m(H);
  vector<vector<pair<long long, long long> > > d(
      H, vector<pair<long long, long long> >(
             W, pair<long long, long long>(1000000000, 1000000000)));
  for (long long i = 0; i < H; ++i) {
    m[i].resize(W);
    for (long long j = 0; j < W; ++j) {
      cin >> m[i][j];
      if (m[i][j] == 'S') {
        s.first = i;
        s.second = j;
        m[i][j] = '.';
      }
    }
  }
  long long ans = 0;
  priority_queue<
      pair<pair<long long, long long>, pair<long long, long long> >,
      vector<pair<pair<long long, long long>, pair<long long, long long> > >,
      greater<pair<pair<long long, long long>, pair<long long, long long> > > >
      q;
  d[s.first][s.second] = pair<long long, long long>(0, 0);
  q.push(pair<pair<long long, long long>, pair<long long, long long> >(
      pair<long long, long long>(0, 0), s));
  while (!q.empty()) {
    pair<pair<long long, long long>, pair<long long, long long> > p = q.top();
    q.pop();
    pair<long long, long long> v = p.second;
    if (v.first == 0 || v.first == H - 1 || v.second == 0 ||
        v.second == W - 1) {
      if (d[v.first][v.second].first % K == 0)
        ans = d[v.first][v.second].first / K - 1;
      else
        ans = d[v.first][v.second].first / K;
      break;
    }
    if (d[v.first][v.second] < p.first) continue;
    for (long long i = 0; i < 4; ++i) {
      long long nf = v.first + dx[i];
      long long ns = v.second + dy[i];
      if (nf >= 0 && nf < H && ns >= 0 && nf < W) {
        if (m[nf][ns] == '.') {
          if (d[nf][ns].first > d[v.first][v.second].first + 1 ||
              (d[nf][ns].first == d[v.first][v.second].first + 1 &&
               d[nf][ns].second > d[v.first][v.second].second)) {
            d[nf][ns].first = d[v.first][v.second].first + 1;
            d[nf][ns].second = d[v.first][v.second].second;
            q.push(
                pair<pair<long long, long long>, pair<long long, long long> >(
                    d[nf][ns], pair<long long, long long>(nf, ns)));
          }
          if (m[nf][ns] == '#') {
            long long hoge;
            if (d[v.first][v.second].first % K == 0)
              hoge = d[v.first][v.second].first / K - 1;
            else
              hoge = d[v.first][v.second].first / K;
            long long fo = d[v.first][v.second].first;
            if (hoge * K == d[v.first][v.second].second) {
              fo = hoge * K + 1;
            }
            if (d[nf][ns].first > fo + 1 ||
                (d[nf][ns].first == fo + 1 &&
                 d[nf][ns].second > d[v.first][v.second].second)) {
              d[nf][ns].first = fo + 1;
              d[nf][ns].second = d[v.first][v.second].second;
              q.push(
                  pair<pair<long long, long long>, pair<long long, long long> >(
                      d[nf][ns], pair<long long, long long>(nf, ns)));
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
}
