#include <bits/stdc++.h>
const int INF = 1e8;
using namespace std;
const int N = 805;
struct Point {
  int x, y;
  bool operator<(const Point &right) const { return x < right.x; }
};
int dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};
char c[N][N];
int m[N][N];
void bfs(int h, int w, Point p) {
  priority_queue<pair<int, Point>, vector<pair<int, Point>>,
                 greater<pair<int, Point>>>
      q;
  for (int i = 0; i < (N); i++)
    for (int j = 0; j < (N); j++) m[i][j] = INF;
  m[p.y][p.x] = 0;
  q.push(make_pair(0, p));
  pair<int, Point> cur;
  while (not q.empty()) {
    cur = q.top();
    q.pop();
    Point u = cur.second;
    for (int i = 0; i < (4); i++) {
      Point next;
      next.x = u.x + dx[i];
      next.y = u.y + dy[i];
      if (next.x < 0 || next.x >= w || next.y < 0 || next.y >= h) continue;
      if (m[next.y][next.x] > m[u.y][u.x] && c[next.y][next.x] == '.') {
        m[next.y][next.x] = m[u.y][u.x];
        q.push(make_pair(m[next.y][next.x], next));
      } else if (m[next.y][next.x] > m[u.y][u.x] + 1 &&
                 c[next.y][next.x] == '#') {
        m[next.y][next.x] = m[u.y][u.x] + 1;
        q.push(make_pair(m[next.y][next.x], next));
      }
    }
  }
}
int bfs(int h, int w, Point p, int k) {
  priority_queue<pair<int, Point>, vector<pair<int, Point>>,
                 greater<pair<int, Point>>>
      q;
  int t = 1;
  bool used[N][N] = {{0}};
  while (true) {
    int dis[N][N];
    for (int i = 0; i < (N); i++)
      for (int j = 0; j < (N); j++) dis[i][j] = INF;
    dis[p.y][p.x] = 0;
    q.push(make_pair(0, p));
    while (not q.empty()) {
      pair<int, Point> cur = q.top();
      q.pop();
      Point u = cur.second;
      used[u.y][u.x] = true;
      for (int i = 0; i < (4); i++) {
        Point next;
        next.x = u.x + dx[i];
        next.y = u.y + dy[i];
        if (next.x < 0 || next.x >= w || next.y < 0 || next.y >= h) continue;
        if (dis[next.y][next.x] > dis[u.y][u.x] + 1 &&
            m[next.y][next.x] <= k * (t - 1) && dis[u.y][u.x] + 1 <= t * k) {
          dis[next.y][next.x] = dis[u.y][u.x] + 1;
          q.push(make_pair(dis[next.y][next.x], next));
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < (h); i++) ans = min(ans, min(dis[i][0], dis[i][w - 1]));
    for (int i = 0; i < (w); i++) ans = min(ans, min(dis[0][i], dis[h - 1][i]));
    if (ans != INF) return t;
    t++;
  }
  return -1;
}
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  Point s;
  for (int i = 0; i < (h); i++) {
    for (int j = 0; j < (w); j++) {
      cin >> c[i][j];
      if (c[i][j] == 'S') s = Point{j, i};
    }
  }
  bfs(h, w, s);
  cout << bfs(h, w, s, k) << endl;
}
