#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
const int N = 805;
char s[N][N];
bool vis[N][N];
struct Node {
  pair<int, int> p;
  int t, left;
  Node() {}
  Node(pair<int, int> p, int t, int left) {
    this->p = p;
    this->t = t;
    this->left = left;
  }
  bool operator<(const Node& tmp) const {
    if (t != tmp.t) return t > tmp.t;
    return left > tmp.left;
  }
};
priority_queue<Node> que;
int main() {
  int h, w, k;
  scanf("%d%d%d", &h, &w, &k);
  pair<int, int> S;
  for (int i = 1; i <= h; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= w; j++)
      if (s[i][j] == 'S') S.first = i, S.second = j;
  }
  for (int i = 1; i <= h; i++) s[i][0] = s[i][w + 1] = '.';
  for (int i = 1; i <= w; i++) s[0][i] = s[h + 1][i] = '.';
  que.push(Node(S, 1, k));
  int ans = 10000000;
  while (!que.empty()) {
    Node cur = que.top();
    que.pop();
    vis[cur.p.first][cur.p.second] = 1;
    if (cur.p.first == 0 || cur.p.first == h + 1 || cur.p.second == 0 ||
        cur.p.second == w + 1) {
      ans = min(ans, cur.t);
      continue;
    }
    if (cur.left == 0) cur.t++, cur.left = k;
    for (int i = 0; i < 4; i++) {
      int x = cur.p.first + dx[i];
      int y = cur.p.second + dy[i];
      if (x >= 0 && x <= h + 1 && y >= 0 && y <= w + 1 && !vis[x][y]) {
        if (cur.t & 1) {
          if (s[x][y] == '#')
            que.push(Node(pair<int, int>(x, y), cur.t + 1, k - 1));
          else
            que.push(Node(pair<int, int>(x, y), cur.t, cur.left - 1));
        } else {
          if (s[x][y] == '.')
            que.push(Node(pair<int, int>(x, y), cur.t + 1, k - 1));
          else
            que.push(Node(pair<int, int>(x, y), cur.t, cur.left - 1));
        }
      }
    }
  }
  ans = (ans + 1) / 2;
  printf("%d\n", ans);
}
