#include <bits/stdc++.h>
using namespace std;
int H, W, K;
void print(bitset<80 * 80> b) {
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      cerr << b[y * W + x];
    }
    cerr << endl;
  }
  cerr << endl;
}
bool check(bitset<80 * 80> s) {
  for (int y = 0; y < H; y++) {
    if (s[y * W + 0]) return true;
    if (s[y * W + W - 1]) return true;
  }
  for (int x = 0; x < W; x++) {
    if (s[0 * W + x]) return true;
    if (s[(H - 1) * W + x]) return true;
  }
  return false;
}
bitset<80 * 80> calc_kabe(bitset<80 * 80> kabe, bitset<80 * 80> s) {
  cerr << "kabe" << endl;
  bitset<80 * 80> ts = s;
  for (int k = 0; k < 80; k++) {
    bitset<80 * 80> ns = bitset<80 * 80>();
    ns |= ts << W;
    ns |= ts << 1;
    ns |= ts >> 1;
    ns |= ts >> W;
    ns &= ~kabe;
    ts |= ns;
  }
  bitset<80 * 80> ns = bitset<80 * 80>();
  ns |= ts << W;
  ns |= ts << 1;
  ns |= ts >> 1;
  ns |= ts >> W;
  bitset<80 * 80> t = ns & kabe;
  return kabe & ~t;
}
int main() {
  cin >> H >> W >> K;
  bitset<80 * 80> kabe = ~bitset<80 * 80>();
  bitset<80 * 80> s = bitset<80 * 80>();
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      char c;
      cin >> c;
      if (c != '#') kabe[y * W + x] = false;
      if (c == 'S') s[y * W + x] = true;
    }
  }
  print(kabe);
  cerr << endl;
  cerr << "s" << endl;
  print(s);
  for (int cnt = 1;; cnt++) {
    cerr << "cnt:" << cnt << endl;
    for (int k = 0; k < K; k++) {
      bitset<80 * 80> ns = bitset<80 * 80>();
      ns |= s << W;
      ns |= s << 1;
      ns |= s >> 1;
      ns |= s >> W;
      ns &= ~kabe;
      s |= ns;
      if (check(s)) {
        cout << cnt << endl;
        return 0;
      }
      print(s);
    }
    cerr << "endk" << endl;
    for (int k = 0; k < K; k++) {
      cerr << "\tk:" << k << endl;
      kabe = calc_kabe(kabe, s);
      if (check(~kabe)) break;
    }
  }
  return 0;
}
