#include <bits/stdc++.h>
using namespace std;
int h, w;
string mp[801];
struct dat {
  int y, x, K1, K2, cost;
};
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
pair<int, pair<int, int> > D[801][801];
int bfs(int y, int x, int sK) {
  for (int i = 0; i < 801; i++)
    for (int j = 0; j < 801; j++)
      D[i][j] = pair<int, pair<int, int> >(1e9, pair<int, int>(1e9, 1e9));
  queue<dat> Q;
  Q.push((dat){y, x, sK, 0, 1});
  D[y][x] = pair<int, pair<int, int> >(sK, pair<int, int>(0, sK));
  int res = 1e9;
  while (!Q.empty()) {
    dat t = Q.front();
    Q.pop();
    y = t.y;
    x = t.x;
    int K1 = t.K1;
    int K2 = t.K2;
    int cost = t.cost;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      int nK1 = K1 - 1;
      int nK2 = K2;
      int ncost = cost;
      if (nx < 0 || ny < 0 || nx >= w || ny >= h) {
        res = min(res, cost);
        continue;
      }
      if (nK1 < 0) ncost++, nK1 = sK - 1, nK2 += sK;
      if (mp[ny][nx] == '#') nK2--;
      if (nK2 < 0) nK1 = nK2 = sK - 1, ncost += 2;
      pair<int, pair<int, int> > tmp =
          pair<int, pair<int, int> >(ncost, pair<int, int>(nK2, nK1));
      if (D[ny][nx] <= tmp) continue;
      Q.push((dat){ny, nx, nK1, nK2, ncost});
      D[ny][nx] = tmp;
    }
  }
  if (res > 2) res--;
  return res;
}
int main() {
  int K;
  cin >> h >> w >> K;
  for (int i = 0; i < h; i++) cin >> mp[i];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (mp[i][j] == 'S') cout << bfs(i, j, K) << endl;
  return 0;
}
