#include <bits/stdc++.h>
using namespace std;
struct BIT {
  std::vector<int> bit;
  BIT(int size) : bit(size, 0) {}
  void add(int k, int x) {
    while (k < bit.size()) {
      bit[k] += x;
      k += k & -k;
    }
  }
  int sum(int k) {
    int res = 0;
    while (k > 0) {
      res += bit[k];
      k -= k & -k;
    }
    return res;
  }
};
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
int h, w, k;
vector<string> wall;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
pair<int, int> start() {
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (wall[y][x] == 'S') {
        return make_pair(x, y);
      }
    }
  }
  return make_pair(0, 0);
}
bool movable(int x, int y) { return wall[y][x] != '#'; }
bool goal(int x, int y) {
  if (x <= 0 || x >= w - 1) {
    return true;
  }
  if (y <= 0 || y >= h - 1) {
    return true;
  }
  return false;
}
vector<vector<int>> buf;
int dist2(int a) {
  auto v = a / k;
  if (a % k > 0) {
    v++;
  }
  return v;
}
int dist(int x, int y) {
  auto left = dist2(x);
  auto right = dist2(w - 1 - x);
  auto top = dist2(y);
  auto bottom = dist2(h - 1 - y);
  return min(left, min(right, min(top, bottom)));
}
int main(void) {
  cin >> h >> w >> k;
  for (int y = 0; y < h; y++) {
    string s;
    cin >> s;
    wall.push_back(s);
  }
  for (int y = 0; y < h; y++) {
    buf.emplace_back(vector<int>(w, -1));
  }
  auto s = start();
  queue<tuple<int, int, int>> q;
  q.push(make_tuple(s.first, s.second, k));
  buf[s.second][s.first] = k;
  bool first = false;
  while (!q.empty()) {
    auto t = q.front();
    q.pop();
    int x, y, c;
    tie(x, y, c) = t;
    auto nc = c - 1;
    if (nc < 0) {
      continue;
    }
    for (int i = 0; i < 4; i++) {
      auto nx = x + dx[i];
      auto ny = y + dy[i];
      if (goal(nx, ny)) {
        first = true;
        continue;
      }
      if (buf[ny][nx] < nc) {
        if (movable(nx, ny)) {
          buf[ny][nx] = nc;
          q.push(make_tuple(nx, ny, nc));
        }
      }
    }
  }
  if (first) {
    cout << 1 << endl;
    return 0;
  }
  int minDist = 100000;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (buf[y][x] >= 0) {
        minDist = min(minDist, dist(x, y) + 1);
      }
    }
  }
  cout << minDist << endl;
  return 0;
}
