#include <bits/stdc++.h>
template <class T>
void chmax(T& a, const T& b) {
  a = max(a, b);
}
template <class T>
void chmin(T& a, const T& b) {
  a = min(a, b);
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
const int mod = 1e9 + 7, INF = 1 << 30;
const double EPS = 1e-12, PI = 3.1415926535897932384626;
const ll lmod = 1e9 + 7, LINF = 1LL << 60;
const int MAX_N = 100005;
string s[802];
int a[802][802];
vi dx = {-1, 0, 1, 0};
vi dy = {0, 1, 0, -1};
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) cin >> s[i];
  queue<P> que1, que2;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      if (s[i][j] == 'S') {
        a[i][j] = 0;
        que1.push(P(i, j));
      }
      a[i][j] = -1;
    }
  for (int ss = 0; ss < 2; ss++) {
    while (!que1.empty()) {
      int i, j;
      tie(i, j) = que1.front();
      if (i == 0 || i == H - 1 || j == 0 || j == W - 1) {
        cout << (a[i][j] - 1) / K + 1 << endl;
        return 0;
      }
      que1.pop();
      for (auto x : dx)
        for (auto y : dy) {
          if (i + x >= 0 && i + x < H && j + y >= 0 && j + y < W &&
              a[i + x][j + y] == -1) {
            if (ss == 0)
              if (s[i + x][j + y] == '#') continue;
            a[i + x][j + y] = a[i][j] + 1;
            if (a[i + x][j + y] != K) que1.push(P(i + x, j + y));
          }
        }
    }
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        if (a[i][j] != -1) {
          a[i][j] = K;
          que1.push(P(i, j));
        }
      }
  }
  return 0;
}
