#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<int> > mp(h, vector<int>(w, 0));
  vector<vector<int> > d3(h, vector<int>(w, -1));
  char c;
  pair<int, int> p;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c;
      if (c == '#') {
        mp[i][j] = 1;
      } else if (c == 'S') {
        mp[i][j] = 0;
        p.first = i;
        p.second = j;
      } else {
        mp[i][j] = 0;
      }
    }
  }
  queue<pair<int, int> > qq;
  qq.push(p);
  d3[p.first][p.second] = 0;
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  vector<pair<int, int> > d4;
  d4.push_back(p);
  int p2 = 0;
  while (true) {
    p2++;
    for (int i = 0; i < k; i++) {
      queue<pair<int, int> > q3;
      while (!qq.empty()) {
        pair<int, int> rr = qq.front();
        int y = rr.first;
        int x = rr.second;
        qq.pop();
        for (int j = 0; j < 4; j++) {
          if (0 <= y + dy[j] && y + dy[j] < h && 0 <= x + dx[j] &&
              x + dx[j] < w && mp[y + dy[j]][x + dx[j]] == 0 &&
              d3[y + dy[j]][x + dx[j]] == -1) {
            d3[y + dy[j]][x + dx[j]] = p2;
            q3.push(pair<int, int>(y + dy[j], x + dx[j]));
            d4.push_back(pair<int, int>(y + dy[j], x + dx[j]));
          }
        }
      }
      qq = q3;
    }
    while (!qq.empty()) {
      qq.pop();
    }
    queue<pair<int, int> > q4;
    for (int i = 0; i < d4.size(); i++) {
      int y = d4[i].first;
      int x = d4[i].second;
      for (int j = 0; j < 4; j++) {
        if (0 <= y + dy[j] && y + dy[j] < h && 0 <= x + dx[j] &&
            x + dx[j] < w && mp[y + dy[j]][x + dx[j]] == 1) {
          q4.push(d4[i]);
          break;
        }
      }
      qq.push(d4[i]);
    }
    for (int i = 0; i < k; i++) {
      queue<pair<int, int> > q5;
      while (!q4.empty()) {
        pair<int, int> rr = q4.front();
        int y = rr.first;
        int x = rr.second;
        q4.pop();
        for (int j = 0; j < 4; j++) {
          if (0 <= y + dy[j] && y + dy[j] < h && 0 <= x + dx[j] &&
              x + dx[j] < w && mp[y + dy[j]][x + dx[j]] == 1) {
            mp[y + dy[j]][x + dx[j]] = 0;
            q5.push(pair<int, int>(y + dy[j], x + dx[j]));
          }
        }
      }
      q4 = q5;
    }
    int ans = 10000;
    for (int i = 0; i < h; i++) {
      if (d3[i][0] != -1) {
        ans = min(ans, d3[i][0]);
      }
    }
    for (int i = 0; i < w; i++) {
      if (d3[0][i] != -1) {
        ans = min(ans, d3[0][i]);
      }
    }
    if (ans != 10000) {
      cout << ans << endl;
      break;
    }
  }
  return 0;
}
