#include <bits/stdc++.h>
using namespace std;
struct node {
  int i, j;
  int left, cast, unlock;
  node(int x, int y, int l, int c, int u)
      : i(x), j(y), left(l), cast(c), unlock(u) {}
};
const int MAXN = 800 + 10;
int newi[] = {-1, +0, +1, +0};
int newj[] = {+0, +1, +0, -1};
int grid[MAXN][MAXN];
bool vis[MAXN][MAXN];
int h, w, k;
int bfs(int a, int b) {
  int res = h + w;
  queue<node> q;
  vis[a][b] = true;
  q.emplace(a, b, k, 1, 0);
  while (!q.empty()) {
    node u = q.front();
    q.pop();
    if (!(u.i) || !(u.j) || !(u.i + 1 - h) || !(u.j + 1 - w))
      res = min(u.cast, res);
    for (int k = 0; k < int(4); k++) {
      int ni = u.i + newi[k], nj = u.j + newj[k];
      if (vis[ni][nj]) continue;
      vis[ni][nj] = true;
      if (grid[ni][nj] == '.') {
        if (u.left)
          q.emplace(ni, nj, u.left - 1, u.cast, u.unlock);
        else
          q.emplace(ni, nj, k - 1, u.cast + 1, u.unlock + k);
      }
      if (grid[ni][nj] == '#') {
        if (u.left && u.unlock)
          q.emplace(ni, nj, u.left - 1, u.cast, u.unlock - 1);
        else
          q.emplace(ni, nj, k - 1, u.cast + 1, k + u.unlock - 1);
      }
    }
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> k;
  pair<int, int> start;
  for (int i = 0; i < int(h); i++) {
    string s;
    cin >> s;
    for (int j = 0; j < int(w); j++) {
      grid[i][j] = s[j];
      vis[i][j] = false;
      if (grid[i][j] == 'S') {
        start = make_pair(i, j);
        grid[i][j] = '.';
      }
    }
  }
  cout << bfs(start.first, start.second) << '\n';
  return 0;
}
