#include <bits/stdc++.h>
using namespace std;
int BT = 10000000, H, W, K;
bool vis[810][810];
string grid[810];
vector<pair<int, int> > v;
const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
int Do(int x, int y) {
  int a = x - 0, b = H - 1 - x, c = y - 0, d = W - 1 - y,
      X = min(min(a, b), min(c, d));
  return X = (X + K - 1) / K;
  ;
}
void dfs(int x, int y, int turn) {
  if (x == 0 || y == 0 || x == H - 1 || y == W - 1) {
    cout << 1 << endl;
    exit(0);
  }
  vis[x][y] = true;
  v.push_back(make_pair(x, y));
  if (turn != K)
    for (int i = 0; i < 4; i++) {
      int tox = x + dx[i], toy = y + dy[i];
      if (vis[tox][toy] || grid[tox][toy] == '#') continue;
      dfs(tox, toy, turn + 1);
    }
  return;
}
int main() {
  cin >> H >> W >> K;
  int X, Y;
  for (int i = 0; i < H; i++) {
    cin >> grid[i];
    for (int j = 0; j < W; j++)
      if (grid[i][j] == 'S') X = i, Y = j;
  }
  dfs(X, Y, 0);
  for (int i = 0; i < v.size(); i++) BT = min(BT, Do(v[i].first, v[i].second));
  cout << BT + 1;
  return 0;
}
