#include <bits/stdc++.h>
const double PI = acos(-1);
const double EPS = 1e-15;
long long INF = (long long)1E17;
long mod(long a) {
  long long c = a % (long long)(1E9 + 7);
  if (c >= 0) return c;
  return c + (long long)(1E9 + 7);
}
using namespace std;
bool prime(int n) {
  if (n == 1) {
    return false;
  } else if (n == 2) {
    return true;
  } else {
    for (int i = 2; i <= sqrt(n); i++) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
}
long long gcd(long long a, long long b) {
  if (a < b) {
    swap(a, b);
  }
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
long long lcm(long long x, long long y) { return (x / gcd(x, y)) * y; }
class UnionFind {
 public:
  vector<int> Parent;
  UnionFind(int N) { Parent = vector<int>(N, -1); }
  int root(int A) {
    if (Parent[A] < 0) return A;
    return Parent[A] = root(Parent[A]);
  }
  int size(int A) { return -Parent[root(A)]; }
  bool connect(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B) {
      return false;
    }
    if (size(A) < size(B)) swap(A, B);
    Parent[A] += Parent[B];
    Parent[B] = A;
    return true;
  }
};
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  for (int i = 0; i < h; i++) {
    cin >> a[i];
  }
  vector<vector<int>> d(h, vector<int>(w, -1));
  queue<pair<int, int>> q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        d[i][j] = 0;
        q.push(make_pair(i, j));
      }
    }
  }
  pair<int, int> p;
  int x, y;
  int nx, ny;
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  while (!q.empty()) {
    p = q.front();
    x = p.first;
    y = p.second;
    q.pop();
    if (d[x][y] == k) continue;
    for (int i = 0; i < 4; i++) {
      nx = x + dx[i];
      ny = y + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && a[nx][ny] == '.') {
        d[nx][ny] = d[x][y] + 1;
        q.push(make_pair(nx, ny));
      }
    }
  }
  int ans = 1000;
  int temp;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (d[i][j] < 0) continue;
      temp = (i + k - 1) / k;
      temp = min(temp, (j + k - 1) / k);
      temp = min(temp, (h - 1 - i + k - 1) / k);
      temp = min(temp, (w - 1 - j + k - 1) / k);
      ans = min(ans, temp + 1);
    }
  }
  return 0;
}
