#include <bits/stdc++.h>
using namespace std;
long long maxd(long long a, long long b) {
  if (a >= b) {
    return a;
  }
  return b;
}
long long mind(long long a, long long b) {
  if (a <= b) {
    return a;
  }
  return b;
}
long long gcd(long long a, long long b) {
  if (a < b) {
    swap(a, b);
  }
  while (b) {
    long long r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
int isPrim(int a) {
  if (a == 1) {
    return 0;
  }
  for (int i = 2; i <= (a + 1) / 2; i++) {
    if (a % i == 0) {
      return 0;
    }
  }
  return 1;
}
long long mod_pow(long long x, long long n, long long mod) {
  long long ret = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      ret = (ret * x) % mod;
    }
    x = (x * x) % mod;
    n = n / 2;
  }
  return ret;
}
struct XX {
  int a;
  int b;
};
class xxIntu {
 public:
  bool operator()(const XX& riLeft, const XX& riRight) const {
    if ((riLeft.a) == (riRight.a)) {
      return riLeft.b < riRight.b;
    }
    return (riLeft.a) < (riRight.a);
  }
};
int ppar[200001];
int rrank[200001];
void init(int n) {
  for (int i = 1; i <= n; i++) {
    ppar[i] = i;
    rrank[i] = 0;
  }
}
int find(int x) {
  if (ppar[x] == x) {
    return x;
  } else {
    return ppar[x] = find(ppar[x]);
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    return;
  }
  if (rrank[x] < rrank[y]) {
    ppar[x] = ppar[y];
  } else {
    ppar[y] = x;
    if (rrank[x] == rrank[y]) {
      rrank[x]++;
    }
  }
}
bool same(int x, int y) { return find(x) == find(y); }
struct edge {
  int u;
  int v;
  int cost;
};
bool comp(edge& e1, edge& e2) { return e1.cost < e2.cost; }
edge es[2000];
int V, E;
int ans = 0;
int kruskal() {
  sort(es, es + E, comp);
  init(V);
  int res = 0;
  for (int i = 0; i < E; i++) {
    edge e = es[i];
    if (!same(e.u, e.v)) {
      unite(e.u, e.v);
      res += e.cost;
      ans++;
    }
  }
  return res;
}
int main(int argc, const char* argv[]) {
  int H, W, K;
  cin >> H >> W >> K;
  string a[800];
  for (int i = 0; i < H; i++) {
    cin >> a[i];
  }
  int x = 0;
  int y = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (a[i].substr(j, 1) == "S") {
        x = j;
        y = i;
      }
    }
  }
  int mat[800][800];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      mat[i][j] = 1000000000;
    }
  }
  mat[x][y] = 0;
  for (int p = 0; p < K; p++) {
    for (int i = 1; i < H - 1; i++) {
      for (int j = 1; j < W - 1; j++) {
        if (mat[i][j] == p) {
          if (a[i - 1].substr(j, 1) == ".") {
            mat[i - 1][j] =
                ((mat[i - 1][j]) < (p + 1) ? (mat[i - 1][j]) : (p + 1));
          }
          if (a[i].substr(j - 1, 1) == ".") {
            mat[i][j - 1] =
                ((mat[i][j - 1]) < (p + 1) ? (mat[i][j - 1]) : (p + 1));
          }
          if (a[i + 1].substr(j, 1) == ".") {
            mat[i + 1][j] =
                ((mat[i + 1][j]) < (p + 1) ? (mat[i + 1][j]) : (p + 1));
          }
          if (a[i].substr(j + 1, 1) == ".") {
            mat[i][j + 1] =
                ((mat[i][j + 1]) < (p + 1) ? (mat[i][j + 1]) : (p + 1));
          }
        }
      }
    }
  }
  int minmat = 1000000000;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (mat[i][j] < K) {
        if (minmat > ((i) < (j) ? (i) : (j))) {
          minmat = ((i) < (j) ? (i) : (j));
        }
        if (minmat > ((H - i) < (W - j) ? (H - i) : (W - j)) - 1) {
          minmat = ((H - i) < (W - j) ? (H - i) : (W - j)) - 1;
        }
      }
    }
  }
  if (minmat == 0) {
    cout << 1 << endl;
  } else {
    ans = minmat / K;
    if (minmat % K != 0) {
      ans++;
    }
    cout << ans + 1 << endl;
  }
  return 0;
}
