#include <bits/stdc++.h>
using namespace std;
int n, m, k, dis[880][880], eu, ev, res, nx, ny;
string grid[880];
queue<pair<int, int> > q;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int F(int x, int y) {
  return (min(min(x - 1, n - x), min(y - 1, m - y)) + k - 1) / k;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> grid[i];
    grid[i] = "?" + grid[i];
  }
  memset(dis, 63, sizeof(dis));
  res = 1e9;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (grid[i][j] == 'S') {
        q.push(make_pair(i, j));
        dis[i][j] = 0;
      }
  while (q.size()) {
    eu = q.front().first;
    ev = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      nx = eu + dx[i];
      ny = ev + dy[i];
      if (grid[nx][ny] == '.' && nx >= 1 && nx <= n && ny >= 1 && ny <= m &&
          dis[nx][ny] > dis[eu][ev] + 1) {
        dis[nx][ny] = dis[eu][ev] + 1;
        q.push(make_pair(eu, ev));
      }
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (dis[i][j] <= k) res = min(res, F(i, j) + 1);
  cout << res << endl;
  return 0;
}
