#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lint = long long;
const lint inf = 1e9 + 7;
const int MOD = 1000000007;
struct Point {
  int x;
  int y;
  vector<Point> next;
  void get_next(vector<vector<int>> const& map) {
    int py = y + 1;
    int px = x;
    if (map[py][px] != inf) {
      Point buf;
      buf.x = px;
      buf.y = py;
      next.push_back(buf);
    }
    py = y - 1;
    px = x;
    if (map[py][px] != inf) {
      Point buf;
      buf.x = px;
      buf.y = py;
      next.push_back(buf);
    }
    py = y;
    px = x + 1;
    if (map[py][px] != inf) {
      Point buf;
      buf.x = px;
      buf.y = py;
      next.push_back(buf);
    }
    py = y;
    px = x - 1;
    if (map[py][px] != inf) {
      Point buf;
      buf.x = px;
      buf.y = py;
      next.push_back(buf);
    }
  }
};
struct Map {
  int h;
  int w;
  pair<int, int> S;
  char road = '.';
  char wall = '#';
  vector<vector<int>> map;
  void mkmap(int in_h, int in_w, vector<string> const& input) {
    h = in_h;
    w = in_w;
    for (int i = 0; i <= h + 1; i++) {
      map.push_back(vector<int>(w + 2, inf));
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (input[i][j] == road) {
          map[i + 1][j + 1]--;
        }
        if (input[i][j] == 'S') {
          S.first = i;
          S.second = j;
        }
      }
    }
  }
  void output() {
    for (int i = 0; i <= h + 1; i++) {
      for (int j = 0; j <= w + 1; j++) {
        if (this->map[i][j] == inf)
          cout << wall;
        else
          cout << map[i][j];
      }
      cout << endl;
    }
  }
  void BFS() {
    Point s;
    s.x = S.first;
    s.y = S.second;
    queue<Point> que;
    vector<vector<int>> memo(h + 2, vector<int>(w + 2, inf));
    que.push(s);
    this->map[s.y][s.x] = 0;
    memo[s.y][s.x] = 0;
    while (!que.empty()) {
      Point buf;
      buf = que.front();
      que.pop();
      buf.get_next(map);
      int num = buf.next.size();
      for (int i = 0; i < num; i++) {
        if (memo[buf.next[i].y][buf.next[i].x] > memo[buf.y][buf.x] + 1) {
          que.push(buf.next[i]);
          memo[buf.next[i].y][buf.next[i].x] = memo[buf.y][buf.x] + 1;
        }
      }
    }
    for (int i = 0; i <= h + 1; i++) {
      for (int j = 0; j <= w + 1; j++) {
        map[i][j] = memo[i][j];
      }
    }
  }
  int map_max() {
    int max = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (map[i][j] != inf) {
          if (map[i][j] > max) max = map[i][j];
        }
      }
    }
    return max;
  }
  int dist(int y, int x) { return map[y][x]; }
  void solve(int k) {
    BFS();
    int res = inf;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        int buf = min(i, w - i - 1);
        buf = min(buf, j);
        buf = min(buf, h - j - 1);
        if (map[i][j] <= k) {
          if (buf == 0) res = min(res, 1);
          if (map[i][j] == 0) {
            res = min(res, (buf - 1) / k + 2);
            if (buf == 0) res = 0;
          }
          buf = (buf - 1) / k + 1;
          res = min(res, buf + 1);
        }
      }
    }
    cout << res << "\n";
  }
};
signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  Map lab;
  vector<string> input(h);
  for (int i = 0; i < h; ++i) cin >> input[i];
  lab.mkmap(h, w, input);
  lab.solve(k);
  return 0;
}
