#include <bits/stdc++.h>
using namespace std;
char cc[1001][1001];
int vis[1001][1001];
int rows, cols, k;
int ans = 123456789;
queue<tuple<int, int, int>> q1, q2;
void try_point(int x, int y, int left, int cnt) {
  if (x <= 0 || y <= 0 || 0x3f3f3f3f != vis[x][y]) return;
  if (0 <= left && 0 == cc[x][y]) {
    vis[x][y] = cnt;
    q1.push(make_tuple(x, y, left));
  } else {
    vis[x][y] = cnt + 1;
    q2.push(make_tuple(x, y, k));
  }
}
void try_point2(int x, int y, int left, int cnt) {
  cc[x][y] = 0;
  if (x <= 0 || y <= 0 || 0x3f3f3f3f != vis[x][y]) return;
  if (0 <= left) {
    vis[x][y] = cnt;
    q2.push(make_tuple(x, y, left));
  } else if (0 == cc[x][y]) {
    vis[x][y] = cnt + 1;
    q1.push(make_tuple(x, y, k));
  }
}
int visit(const int xroot, const int yroot) {
  q1.push(make_tuple(xroot, yroot, k));
  int ret = 1;
  for (;;) {
    ++ret;
    while (!q1.empty()) {
      const auto &t = q1.front();
      const int x = get<0>(t), y = get<1>(t), left0 = get<2>(t);
      if (1 == x || 1 == y || rows == x || cols == y) return ret;
      q1.pop();
      try_point(x - 1, y, left0 - 1, ret);
      try_point(x + 1, y, left0 - 1, ret);
      try_point(x, y - 1, left0 - 1, ret);
      try_point(x, y + 1, left0 - 1, ret);
    }
    ++ret;
    while (!q2.empty()) {
      const auto &t = q2.front();
      const int x = get<0>(t), y = get<1>(t), left0 = get<2>(t);
      if (1 == x || 1 == y || rows == x || cols == y) return ret;
      q2.pop();
      try_point2(x - 1, y, left0 - 1, ret);
      try_point2(x + 1, y, left0 - 1, ret);
      try_point2(x, y - 1, left0 - 1, ret);
      try_point2(x, y + 1, left0 - 1, ret);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int x = 0, y = 0;
  cin >> rows >> cols >> k;
  {
    string s;
    for (int i = 1; i <= rows; i++) {
      cin >> s;
      for (int j = 0; j < cols; j++) {
        if ('#' == s[j])
          cc[i][j + 1] = 1;
        else if ('S' == s[j]) {
          x = i;
          y = j + 1;
        }
      }
    }
    memset(vis, 0x3f, sizeof(vis));
    vis[x][y] = 1;
  }
  ans = visit(x, y);
  cout << ans - 1 << "\n";
  return 0;
}
