#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
  int h, w, K;
  scanf("%d%d%d", &h, &w, &K);
  char a[h][w];
  int sx, sy;
  for (int((i)) = (0); ((i)) < ((h)); ++((i)))
    for (int((j)) = (0); ((j)) < ((w)); ++((j))) {
      scanf(" %c", &a[i][j]);
      if (a[i][j] == 'S') sx = i, sy = j;
    }
  const int INF = 1000000;
  pair<int, int> d[h][w];
  for (int((i)) = (0); ((i)) < ((h)); ++((i)))
    for (int((j)) = (0); ((j)) < ((w)); ++((j)))
      d[i][j] = pair<int, int>(INF, INF);
  queue<pair<pair<int, int>, pair<int, int> > > que;
  d[sx][sy] = pair<int, int>(1, 0);
  que.push(pair<pair<int, int>, pair<int, int> >(pair<int, int>(sx, sy),
                                                 pair<int, int>(1, 0)));
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  while (!que.empty()) {
    pair<pair<int, int>, pair<int, int> > p = que.front();
    que.pop();
    int x = p.first.first;
    int y = p.first.second;
    int td = p.second.first;
    int tk = p.second.second;
    if (d[x][y] < p.second) continue;
    for (int((i)) = (0); ((i)) < ((4)); ++((i))) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w) {
        pair<int, int> nd;
        if (tk == K || (a[x][y] != '#' && a[nx][ny] == '#')) {
          nd = pair<int, int>(td + 1, 1);
        } else
          nd = pair<int, int>(td, tk + 1);
        if (d[nx][ny] > nd) {
          d[nx][ny] = nd;
          que.push(pair<pair<int, int>, pair<int, int> >(pair<int, int>(nx, ny),
                                                         nd));
        }
      }
    }
  }
  int ans = INF;
  for (int((i)) = (0); ((i)) < ((w)); ++((i)))
    ans = min({ans, d[0][i].first, d[h - 1][i].first});
  for (int((i)) = (0); ((i)) < ((h)); ++((i)))
    ans = min({ans, d[i][0].first, d[i][w - 1].first});
  printf("%d\n", ans);
}
