#include <bits/stdc++.h>
using namespace std;
vector<string> A;
bool used[805][805];
int main(void) {
  int h, w, k;
  cin >> h >> w >> k;
  int memok = k;
  string s;
  for (int i = 0; i < (int)(h); i++) {
    cin >> s;
    A.push_back(s);
  }
  for (int i = 0; i < (int)(h); i++) {
    for (int j = 0; j < (int)(w); j++) {
      used[i][j] = false;
    }
  }
  bool me = true;
  int mi, mj;
  for (int i = 0; i < (int)(h); i++) {
    for (int j = 0; j < (int)(w); j++) {
      if (A[i][j] == 'S') {
        me = false;
        mi = i;
        mj = j;
      }
      if (!me) {
        break;
      }
    }
    if (!me) {
      break;
    }
  }
  queue<pair<int, int> > Q;
  pair<int, int> p2;
  p2.first = mi;
  p2.second = mj;
  Q.push(p2);
  while (Q.size() > 0 && k > 0) {
    pair<int, int> p = Q.front();
    Q.pop();
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (abs(i) + abs(j) != 1) continue;
        if (p.first + i < 0 || p.first + i >= h || p.second + j < 0 ||
            p.second + j > w)
          continue;
        if (!used[p.first + i][p.second + j] &&
            A[p.first + i][p.second + j] != '#') {
          pair<int, int> next;
          next.first = p.first + i;
          next.second = p.second + j;
          Q.push(next);
          used[p.first + i][p.second + j] = true;
        }
      }
    }
    k--;
  }
  int ans =
      min((int)(1123456789), min(mi, min(h - mi - 1, min(mj, w - mj - 1))));
  for (int i = 0; i < (int)(h); i++) {
    for (int j = 0; j < (int)(w); j++) {
      if (used[i][j]) {
        ans = min(ans, min(i, min(h - i - 1, min(j, w - j - 1))));
      }
    }
  }
  cout << 1 + (ans / memok) + (ans % memok == 0 ? 0 : 1) << endl;
  return 0;
}
