#include <bits/stdc++.h>
using namespace std;
int n, m, k, X, Y;
char a[1005][1005];
bool visit[1005][1005];
vector<pair<int, int> > v;
void dfs(int x, int y, int s) {
  if (x <= 0 || y <= 0 || x > n || y > m || visit[x][y] || a[x][y] != '.' ||
      s > k)
    return;
  visit[x][y] = 1;
  v.push_back(make_pair(x, y));
  dfs(x + 1, y, s + 1);
  dfs(x, y + 1, s + 1);
  dfs(x, y - 1, s + 1);
  dfs(x - 1, y, s + 1);
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        a[i][j] = '.';
        X = i, Y = j;
      }
    }
  }
  dfs(X, Y, 0);
  int mi = (1000000007);
  for (int i = 0; i < v.size(); i++) {
    X = v[i].first, Y = v[i].second;
    mi = min(mi, n - X);
    mi = min(mi, m - Y);
    mi = min(mi, Y - 1);
    mi = min(mi, X - 1);
  }
  cout << 1 + (mi + k - 1) / k;
  return 0;
}
