#include <bits/stdc++.h>
using namespace std;
const int MAXN = 888;
const int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int n, m, k, ans = INT_MAX;
char g[MAXN][MAXN];
bool vis[MAXN][MAXN];
struct State {
  int x, y, mv, unlck, step;
  State(int x = 0, int y = 0, int mv = 0, int unlck = 0, int step = 0)
      : x(x), y(y), mv(mv), unlck(unlck), step(step) {}
};
bool inRange(int x, int y) { return (x >= 0 && y >= 0 && x < n && y < m); }
void bfs(int bx, int by) {
  queue<State> q;
  while (!q.empty()) q.pop();
  q.push(State(bx, by, 0, 0, 0));
  vis[bx][by] = 1;
  while (!q.empty()) {
    State u = q.front();
    q.pop();
    if (u.x == 0 || u.y == 0 || u.x == n - 1 || u.y == m - 1) {
      ans = min(ans, u.step);
    }
    for (int i = 0; i < 4; i++) {
      int tx = u.x + dir[i][0], ty = u.y + dir[i][1];
      if (inRange(tx, ty) && !vis[tx][ty]) {
        vis[tx][ty] = 1;
        int stp = u.step;
        if (g[tx][ty] == '.') {
          if (stp * k < u.mv + 1) stp++;
          q.push(State(tx, ty, u.mv + 1, u.unlck, stp));
        }
        if (g[tx][ty] == '#') {
          if (stp * k < u.mv + 1 || stp * k < u.unlck + 1) stp++;
          if (u.mv < u.unlck + k) stp++;
          q.push(State(tx, ty, u.mv + 1, u.unlck + 1, stp));
        }
      }
    }
  }
}
int main() {
  cin >> n >> m >> k;
  int bx = 0, by = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> g[i][j];
      if (g[i][j] == 'S') {
        bx = i;
        by = j;
        g[i][j] = '.';
      }
    }
  }
  bfs(bx, by);
  cout << ans << endl;
  return 0;
}
