#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B>
bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-9;
int inf = sizeof(int) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, m, k, out;
int f(int x, int y) {
  x = min(x, n - x - 1), y = min(y, m - y - 1);
  if (x == 0 || y == 0) return 0;
  int a = (x - 1) / k;
  int b = (y - 1) / k;
  return min(a, b) + 1;
}
vector<vector<int> > used;
vector<string> in;
void dfs(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
    if (used[nx][ny]) continue;
    if (in[nx][ny] == '#') continue;
    used[nx][ny] = true;
    out = min(out, f(nx, ny));
    f(nx, ny);
  }
}
int main() {
  cin >> n >> m >> k;
  out = inf;
  in = vector<string>(n);
  for (int i = 0; i < n; i++) cin >> in[i];
  used = vector<vector<int> >(n, vector<int>(m));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (in[i][j] == 'S') {
        in[i][j] = '!';
        used[i][j] = true;
        out = f(i, j);
        dfs(i, j);
      }
  cout << out + 1 << endl;
}
