#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int mod998 = 998244353;
struct T {
  int x, y, t;
};
const int X[4] = {0, 0, -1, 1}, Y[4] = {-1, 1, 0, 0};
int H, W, K, x, y, a[800][800], MIN;
char C[800][800];
queue<T> q;
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      a[i][j] = -1;
      cin >> C[i][j];
      if (C[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
  }
  q.push({x, y, 0});
  while (!q.empty()) {
    T t = q.front();
    q.pop();
    if (a[t.x][t.y] == -1) {
      a[t.x][t.y] = t.t;
      for (int i = 0; i < 4; ++i) {
        int XX = t.x + X[i], YY = t.y + Y[i];
        if (XX < 0 || H <= XX || YY < 0 || W <= YY || C[XX][YY] != '.' ||
            a[XX][YY] != -1)
          continue;
        q.push({XX, YY, t.t + 1});
      }
    }
  }
  MIN = 2147483647;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (a[i][j] != -1 && a[i][j] <= K) {
        MIN = min({MIN, i, j, H - 1 - i, W - 1 - j});
      }
    }
  }
  if (MIN == 0)
    cout << 0 << endl;
  else
    cout << ((MIN - 1) / K + 2) << endl;
  return 0;
}
