#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const long long MOD = 1e9 + 7;
const double EPS = 1e-9;
const int INF = 1e9;
long long addMod(long long a, long long b) {
  if (a + b >= MOD) return a + b - MOD;
  return a + b;
}
long long mulMod(long long a, long long b) { return (a * b) % MOD; }
long long fast(long long a, long long b) {
  if (b == 0) return 1;
  long long tmp = fast(a, b / 2);
  tmp = mulMod(tmp, tmp);
  if (b & 1) tmp = mulMod(tmp, a);
  return tmp;
}
long long GCD(long long a, long long b) {
  if (a > b) return GCD(b, a);
  if (a == 0) return b;
  return GCD(b % a, a);
}
const int N = 800;
int dy[] = {-1, 1, 0, 0};
int dx[] = {0, 0, -1, 1};
pair<int, int> st;
int r, c, k;
string M[N + 5];
bool vis[N + 5][N + 5];
bool inRange(int y, int x) { return y >= 0 && y < r && x >= 0 && x < c; }
int solve() {
  priority_queue<pair<int, pair<pair<int, int>, pair<int, int> > > > q;
  while (!q.empty()) q.pop();
  q.push(make_pair(0, make_pair(st, make_pair(0, 0))));
  int y, x, yy, xx, step, move, del;
  while (!q.empty()) {
    y = q.top().second.first.first;
    x = q.top().second.first.second;
    step = -q.top().first;
    move = q.top().second.second.first;
    del = q.top().second.second.second;
    q.pop();
    if (vis[y][x]) continue;
    if (y == 0 || y == r - 1 || x == 0 || x == c - 1) return step;
    vis[y][x] = true;
    if (move == 0) {
      move = k;
      del += k;
      step++;
    }
    for (int i = 0; i <= (int)4 - 1; i += 1) {
      yy = y + dy[i];
      xx = x + dx[i];
      if (inRange(yy, xx) && !vis[yy][xx]) {
        if (M[yy][xx] == '#') {
          if (del > k)
            q.push(make_pair(-step, make_pair(make_pair(yy, xx),
                                              make_pair(move - 1, del - 1))));
          else
            q.push(make_pair(
                -(step + 1),
                make_pair(make_pair(yy, xx), make_pair(k - 1, k + del - 1))));
        } else {
          q.push(make_pair(
              -step, make_pair(make_pair(yy, xx), make_pair(move - 1, del))));
        }
      }
    }
  }
  return 0;
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> r >> c >> k;
  for (int i = 0; i <= (int)r - 1; i += 1) {
    cin >> M[i];
    for (int j = 0; j <= (int)c - 1; j += 1) {
      if (M[i][j] == 'S') {
        st = make_pair(i, j);
      }
    }
  }
  cout << solve() << endl;
  return 0;
}
