#include <bits/stdc++.h>
using namespace std;
char MP[1024][1024];
bool visit[1024][1024];
int dist[1024][1024];
int n, m, k;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
void bfs(int x, int y) {
  queue<pair<int, int> > Q;
  Q.push({x, y});
  dist[x][y] = 0;
  visit[x][y] = 1;
  while (Q.size()) {
    pair<int, int> c = Q.front();
    Q.pop();
    int x = c.first;
    int y = c.second;
    for (int i = 0; i < n; i++) {
      if (!(x + dx[i] < 1 || x + dx[i] > n || y + dy[i] < 1 || y + dy[i] > m) &&
          !visit[x + dx[i]][y + dy[i]] && MP[x + dx[i]][y + dy[i]] != '#') {
        visit[x + dx[i]][y + dy[i]] = 1;
        Q.push({x + dx[i], y + dy[i]});
        dist[x + dx[i]][y + dy[i]] = dist[x][y] + 1;
      }
    }
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) cin >> (MP[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) dist[i][j] = 1e9;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (MP[i][j] == 'S') {
        bfs(i, j);
      }
    }
  int res = 1e9;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (dist[i][j] < 1e9) {
        int s = min(min(i - 1, n - i), min(j - 1, m - j));
        res = min(res, max((dist[i][j] + k - 1) / k, 1) + (s + k - 1) / k);
      }
    }
  }
  cout << res + 1 << endl;
  return 0;
}
