#include <bits/stdc++.h>
using namespace std;
const int N = 5050;
int n, m, ans = N;
bool vis[N][N];
char mp[N][N];
void update(int x, int y) {
  vis[x][y] = 1;
  ans = min(ans, y);
  ans = min(ans, m - 1 - y);
  ans = min(ans, x);
  ans = min(ans, n - 1 - x);
}
struct Point {
  int x, y, k;
  Point(int tx, int ty, int tk) {
    x = tx;
    y = ty;
    k = tk;
  }
};
stack<Point> s;
void dfs(int x, int y, int k) {
  s.push(Point(x, y, k));
  while (!s.empty()) {
    Point a = s.top();
    s.pop();
    x = a.x;
    y = a.y;
    k = a.k;
    if (k < 0 || x < 0 || x >= n || y < 0 || y >= m || mp[x][y] == '#' ||
        vis[x][y] == 1)
      continue;
    update(x, y);
    s.push(Point(x - 1, y, k - 1));
    s.push(Point(x + 1, y, k - 1));
    s.push(Point(x, y - 1, k - 1));
    s.push(Point(x, y + 1, k - 1));
  }
}
int main() {
  int k, x, y;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) scanf("%s", mp[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (mp[i][j] == 'S') {
        x = i;
        y = j;
        break;
      }
  dfs(x, y, k);
  if (ans)
    printf("%d\n", ((ans - 1) / k) + 2);
  else
    printf("1\n");
  return 0;
}
