#include <bits/stdc++.h>
using namespace std;
int rs[] = {0, 0, 1, -1};
int cs[] = {-1, 1, 0, 0};
typedef struct node {
  pair<int, int> tp;
  int r, ml, cl;
} node;
typedef node* Node;
class cmp {
 public:
  bool operator()(Node a, Node b) {
    return (b->r < a->r || (b->r == a->r && b->cl > a->cl));
  }
};
Node newNode(pair<int, int> tp, int r, int ml, int cl) {
  Node tn = (Node)malloc(sizeof(node));
  tn->tp = tp, tn->r = r, tn->ml = ml, tn->cl = cl;
  return tn;
}
int main() {
  int H, W, K, i, r, c;
  int res = 2000000000;
  pair<int, int> tp;
  string m[800];
  cin >> H >> W >> K;
  for (r = 0; r < H; r++) {
    cin >> m[r];
    for (c = 0; c < W; c++) {
      if (m[r][c] == 'S') {
        m[r][c] = '.';
        tp = pair<int, int>(r, c);
      }
    }
  }
  priority_queue<Node, vector<Node>, cmp> q;
  q.push(newNode(tp, 0, K, K));
  int seen[800][800] = {};
  while (!q.empty()) {
    Node t = q.top();
    q.pop();
    if ((t->tp.first < 0 || t->tp.first >= H || t->tp.second < 0 ||
         t->tp.second >= W)) {
      res = min(res, t->r + 1 + (t->cl != K));
      continue;
    }
    if (seen[t->tp.first][t->tp.second]) continue;
    if (t->ml && t->cl) seen[t->tp.first][t->tp.second] = 1;
    for (i = 0; i < 4; i++) {
      tp = pair<int, int>(t->tp.first + rs[i], t->tp.second + cs[i]);
      if ((tp.first < 0 || tp.first >= H || tp.second < 0 || tp.second >= W) ||
          m[tp.first][tp.second] == '.') {
        Node nn = newNode(tp, t->r, t->ml - 1, t->cl);
        if (nn->ml < 0) {
          nn->ml += K, nn->cl += K, nn->r++;
        }
        q.push(nn);
      } else {
        Node nn = newNode(tp, t->r, t->ml - 1, t->cl - 1);
        if (nn->ml < 0 || nn->cl < 0) {
          nn->ml += K, nn->cl += K, nn->r++;
        }
        q.push(nn);
      }
    }
  }
  cout << res << '\n';
}
