#include <bits/stdc++.h>
using namespace std;
int h, w, k;
int dp[810][810], cop[810][810];
bool t[810][810];
bool ret;
void DFS(int y, int x, int len, int energy) {
  if (y == 0 || y == h - 1 || x == 0 || x == w - 1) {
    ret = true;
    return;
  }
  if (dp[y][x] < energy) dp[y][x] = energy;
  if (len == k) return;
  int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
  for (int i = 0; i < 4; i++) {
    int f = dy[i] + y, g = dx[i] + x;
    int en = energy;
    if (t[f][g] == 0) en--;
    if (en >= 0) DFS(f, g, len + 1, en);
  }
}
int main() {
  cin >> h >> w >> k;
  int f, g;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char z;
      cin >> z;
      if (z == '#')
        t[i][j] = 0;
      else
        t[i][j] = 1;
      if (z == 'S') f = i, g = j;
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cop[i][j] = -1;
    }
  }
  cop[f][g] = 0;
  ret = false;
  int ans = 1;
  while (true) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        dp[i][j] = cop[i][j];
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (cop[i][j] != -1) {
          DFS(i, j, 0, cop[i][j]);
          if (ret) goto loop;
        }
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cop[i][j] = dp[i][j] + k;
      }
    }
    ans++;
  }
loop:
  cout << ans << endl;
  return 0;
}
