#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7;
const int inf = (int)1e9 + 123;
const long long infl = (long long)1e18 + 123;
const double eps = 1e-10;
const int MAX_N = (int)3e5 + 123;
int n, m, k;
char a[1005][1005];
set<pair<int, pair<int, int> > > q;
int dist[1005][1005];
int moves[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
vector<pair<int, int> > st;
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 0; i < n; i++) {
    getchar();
    for (int j = 0; j < m; j++) a[i][j] = getchar();
  }
  pair<int, int> second;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      dist[i][j] = (a[i][j] == 'S' ? 0 : inf);
      if (a[i][j] == 'S') {
        second = make_pair(i, j);
        q.insert(make_pair(dist[i][j], make_pair(i, j)));
      }
    }
  if (second.first == 0 || second.first == n - 1 || second.second == 0 ||
      second.second == m - 1) {
    puts("0");
    return 0;
  }
  for (int ans = 1;; ans++) {
    st.clear();
    while (!q.empty()) {
      pair<int, int> v = q.begin()->second;
      q.erase(q.begin());
      st.push_back(v);
      for (int i = 0; i < 4; i++) {
        pair<int, int> nw =
            make_pair(v.first + moves[i][0], v.second + moves[i][1]);
        if (nw.first >= 0 && nw.first < n && nw.second >= 0 && nw.second < m &&
            dist[nw.first][nw.second] >
                dist[v.first][v.second] + (a[nw.first][nw.second] == '#') &&
            dist[v.first][v.second] + (a[nw.first][nw.second] == '#') <=
                (ans - 1) * k) {
          q.erase(make_pair(dist[nw.first][nw.second], nw));
          dist[nw.first][nw.second] =
              dist[v.first][v.second] + (a[nw.first][nw.second] == '#');
          q.insert(make_pair(dist[nw.first][nw.second], nw));
        }
      }
    }
    for (auto i : st) {
      if (i.first == 0 || i.second == 0 || i.first == n - 1 ||
          i.second == m - 1) {
        printf("%d\n", ans);
        return 0;
      }
      bool have = 0;
      for (int it = 0; it < 4; it++) {
        pair<int, int> nw =
            make_pair(i.first + moves[it][0], i.second + moves[it][1]);
        if (dist[nw.first][nw.second] == inf) {
          have = 1;
          break;
        }
      }
      if (have) q.insert(make_pair(dist[i.first][i.second], i));
    }
  }
  return 0;
}
