#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 10000;
const long long int INF = 1000000010;
const long long int LINF = 1000000000000000010;
int dx[4] = {1, 1, -1, -1};
int dy[4] = {1, -1, -1, 1};
int h, w, k;
bool field(int x, int y) {
  if (x < 1 || x > w || y < 1 || y > h) {
    return false;
  }
  return true;
}
int main() {
  cin >> h >> w >> k;
  char a[810][810];
  int sx, sy;
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= w; j++) {
      a[i][j] = s[j - 1];
      if (a[i][j] == 'S') {
        sx = j;
        sy = i;
        break;
      }
    }
  }
  queue<pair<long long int, long long int> > q;
  q.push(pair<long long int, long long int>(sx, sy));
  int ans[810][810] = {};
  ans[sy][sx] = 1;
  for (int i = 1; i <= k; i++) {
    for (int j = -i; j <= 0; j++) {
      int ny = sy + j;
      int nx = sx - i - j;
      if (field(nx, ny) && ans[ny][nx] == 0 && a[ny][nx] == '.') {
        ans[ny][nx] = 1;
        q.push(pair<long long int, long long int>(nx, ny));
      }
    }
    for (int j = 0; j <= i; j++) {
      int ny = sy + j;
      int nx = sx + i - j;
      if (field(nx, ny) && ans[ny][nx] == 0 && a[ny][nx] == '.') {
        ans[ny][nx] = 1;
        q.push(pair<long long int, long long int>(nx, ny));
      }
    }
  }
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 1; i <= k; i++) {
      for (int j = -i; j <= 0; j++) {
        int ny = y + j;
        int nx = x - i - j;
        if (field(nx, ny) && ans[ny][nx] == 0) {
          ans[ny][nx] = ans[y][x] + 1;
          q.push(pair<long long int, long long int>(nx, ny));
        }
      }
      for (int j = 0; j <= i; j++) {
        int ny = y + j;
        int nx = x + i - j;
        if (field(nx, ny) && ans[ny][nx] == 0) {
          ans[ny][nx] = ans[y][x] + 1;
          q.push(pair<long long int, long long int>(nx, ny));
        }
      }
    }
  }
  int res = INF;
  for (int i = 1; i <= w; i++) {
    res = min(res, min(ans[1][i], ans[h][i]));
  }
  for (int i = 1; i <= h; i++) {
    res = min(res, min(ans[i][1], ans[i][w]));
  }
  cout << res << endl;
  return 0;
}
