#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
constexpr int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
std::vector<std::string> s;
int h, w, sx, sy;
long k;
bool check(long stock) {
  stock *= k;
  std::queue<std::pair<long, std::pair<long, std::pair<int, int>>>> qu;
  qu.push({stock, {stock - k, {sx, sy}}});
  while (!qu.empty()) {
    auto p = qu.front();
    qu.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.second.second.first + vy[i],
          ny = p.second.second.second + vx[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
      if (s[nx][ny] == '#' and !p.second.first) continue;
      if (nx == 0 or nx == h - 1 or ny == 0 or ny == w - 1) return true;
      if (p.first > 0)
        qu.push({p.first - 1, {p.second.first - (s[nx][ny] == '#'), {nx, ny}}});
    }
  }
  return false;
}
long binary() {
  long ok = 2e9, ng = 0;
  while (abs(ok - ng) > 1) {
    long mid = (ok + ng) / 2;
    if (check(mid))
      ok = mid;
    else
      ng = mid;
  }
  return ok;
}
int main() {
  cin >> h >> w >> k;
  s.resize(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
    for (int j = 0; j < w; j++) {
      if (s[i][j] != 'S') continue;
      sx = i;
      sy = j;
      break;
    }
  }
  cout << binary() << endl;
  return 0;
}
