#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template <typename T>
using VT = vector<T>;
template <typename T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const int MAX = 888;
int H, W, K;
string A[MAX];
char strbuf[MAX];
int minCost[MAX][MAX][2][2];
string next() {
  scanf("%s", strbuf);
  return strbuf;
}
const int dh[] = {0, -1, 0, 1};
const int dw[] = {1, 0, -1, 0};
bool inside(int h, int w) { return 0 <= h && h < H && 0 <= w && w < W; }
bool inside(PII p) { return inside(p.first, p.second); }
bool isGoal(int h, int w) {
  return h == 0 || h == H - 1 || w == 0 || w == W - 1;
}
int main(void) {
  for (int(i) = 0; (i) < (MAX); (i)++)
    for (int(j) = 0; (j) < (MAX); (j)++)
      for (int(k) = 0; (k) < (2); (k)++)
        for (int(l) = 0; (l) < (2); (l)++) minCost[i][j][k][l] = INF;
  scanf("%d%d%d", &H, &W, &K);
  PII start;
  for (int(i) = 0; (i) < (H); (i)++) {
    A[i] = next();
    assert(A[i].length() == W);
    for (int(j) = 0; (j) < (W); (j)++) {
      if (A[i][j] == 'S') {
        start = make_pair(i, j);
      }
    }
  }
  priority_queue<tuple<int, PII, int, int>, VT<tuple<int, PII, int, int>>,
                 greater<tuple<int, PII, int, int>>>
      pq;
  pq.push(make_tuple(0, start, 0, 0));
  while (!pq.empty()) {
    PII nowPoint;
    int nowCost, canMove, canOpen;
    tie(nowCost, nowPoint, canMove, canOpen) = pq.top();
    pq.pop();
    if (minCost[nowPoint.first][nowPoint.second][canMove != 0][canOpen != 0] <=
        nowCost) {
      continue;
    }
    chmin(minCost[nowPoint.first][nowPoint.second][canMove != 0][canOpen != 0],
          nowCost);
    for (int(i) = 0; (i) < (4); (i)++) {
      PII nextPoint =
          make_pair(nowPoint.first + dh[i], nowPoint.second + dw[i]);
      if (!inside(nextPoint)) {
        continue;
      }
      if (A[nextPoint.first][nextPoint.second] == '#') {
        if (canOpen == 0) {
          if (canMove == 0) {
            pq.push(make_tuple(nowCost + 2, nextPoint, K - 1, K + K - 1));
          } else {
            pq.push(make_tuple(nowCost + 1, nextPoint, K - 1, K - 1));
          }
        } else {
          if (canMove == 0) {
            pq.push(make_tuple(nowCost + 1, nextPoint, K - 1, canOpen + K - 1));
          } else {
            pq.push(make_tuple(nowCost, nextPoint, canMove - 1, canOpen - 1));
          }
        }
      } else {
        if (canMove == 0)
          pq.push(make_tuple(nowCost + 1, nextPoint, K - 1, canOpen + K));
        else
          pq.push(make_tuple(nowCost, nextPoint, canMove - 1, canOpen));
      }
    }
  }
  int ans = INF;
  for (int(i) = 0; (i) < (H); (i)++) {
    if (i == 0 || i == H - 1) {
      for (int(j) = 0; (j) < (W); (j)++) {
        for (int(k) = 0; (k) < (2); (k)++)
          for (int(l) = 0; (l) < (2); (l)++) chmin(ans, minCost[i][j][k][l]);
      }
    } else {
      for (int(k) = 0; (k) < (2); (k)++)
        for (int(l) = 0; (l) < (2); (l)++) {
          chmin(ans, minCost[i][0][k][l]);
          chmin(ans, minCost[i][W - 1][k][l]);
        }
    }
  }
  cout << ans << endl;
}
