#include <bits/stdc++.h>
using namespace std;
int H, W, K;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
void print(vector<vector<char> > b) {
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      cerr << (int)b[y][x];
    }
    cerr << endl;
  }
  cerr << endl;
}
int dist(int y, int x) {
  int mi = 9999;
  mi = min(y, mi);
  mi = min(H - 1 - y, mi);
  mi = min(x, mi);
  mi = min(W - 1 - x, mi);
  return mi;
}
bool check(int y, int x) {
  return (y == 0 || y == H - 1 || x == 0 || x == W - 1);
}
bool check(pair<int, int> p) { return check(p.first, p.second); }
int main() {
  int a[2];
  cin >> H >> W >> K;
  vector<vector<char> > kabe(H, vector<char>(W, 0));
  vector<vector<char> > s(H, vector<char>(W, 0));
  deque<pair<int, int> > pos;
  pair<int, int> start;
  for (int y = (0); y < (H); ++y) {
    for (int x = (0); x < (W); ++x) {
      char c;
      cin >> c;
      if (c == '#') kabe[y][x] = 1;
      if (c == 'S') {
        s[y][x] = 1;
        start = pair<int, int>(y, x);
        pos.push_back(start);
      }
    }
  }
  print(kabe);
  cerr << endl;
  cerr << "s" << endl;
  print(s);
  int mi = 9999;
  for (int k = (0); k < (K); ++k) {
    deque<pair<int, int> > tpos;
    while (!pos.empty()) {
      pair<int, int> tp = pos.front();
      pos.pop_front();
      for (int i = (0); i < (4); ++i) {
        int ty = tp.first + dy[i];
        int tx = tp.second + dx[i];
        if (s[ty][tx] == 0) {
          if (kabe[ty][tx] == 0) {
            tpos.push_back(pair<int, int>(ty, tx));
            s[ty][tx] = 1;
            mi = dist(ty, tx);
            if (check(ty, tx)) {
              cout << 1 << endl;
              return 0;
            }
          }
        }
      }
      tpos.push_back(tp);
    }
    while (!tpos.empty()) {
      pair<int, int> tp = tpos.front();
      pos.push_back(tp);
      tpos.pop_front();
    }
  }
  cout << (mi - 1) / K + 1 + 1 << endl;
  return 0;
}
