#include <bits/stdc++.h>
using namespace std;
string s[800];
int d[800][800];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
    for (int j = 0; j < w; j++) {
      d[i][j] = 1e9;
      if (s[i][j] == 'S') sx = i, sy = j;
    }
  }
  queue<pair<int, int> > P;
  P.push(make_pair(sx, sy));
  d[sx][sy] = 0;
  int ans = 1e9;
  while (!P.empty()) {
    pair<int, int> p = P.front();
    P.pop();
    if (p.first == 0 || p.second == 0 || p.first == h - 1 ||
        p.second == w - 1) {
      ans = min(ans, d[p.first][p.second]);
      continue;
    }
    for (int r = 0; r < 4; r++) {
      int tx = p.first + dx[r], ty = p.second + dy[r];
      int cost = (s[tx][ty] == '#' ? 2000 : 1);
      if (d[p.first][p.second] + cost < d[tx][ty]) {
        d[tx][ty] = d[p.first][p.second] + cost;
        P.push(make_pair(tx, ty));
      }
    }
  }
  int open = ans / 2000;
  int hosuu = ans % 2000;
  if (open > hosuu)
    cout << (open + k - 1) / k + 1 << endl;
  else
    cout << (hosuu + k - 1) / k << endl;
}
