#include <bits/stdc++.h>
using namespace std;
int n, m, k, sx, sy;
int g[1000][1000];
int vis[1000][1000];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int ans;
bool get(int x, int y) {
  return (x >= 0 && y >= 0 && x < n && y < m && g[x][y] && vis[x][y] == -1);
}
int calc(int x, int y) {
  int a = min(x, y);
  a = min(a, min(n - 1 - x, m - 1 - y));
  return a / k + ((a % k) > 0);
}
int bfs() {
  ans = 100000;
  queue<pair<int, int> > Q;
  Q.push(make_pair(sx, sy));
  memset(vis, -1, sizeof vis);
  vis[sx][sy] = 0;
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    Q.pop();
    if (vis[x][y] == k) continue;
    ans = min(ans, 1 + calc(x, y));
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (get(xx, yy)) {
        vis[xx][yy] = vis[x][y] + 1;
        Q.push(make_pair(xx, yy));
      }
    }
  }
  return ans;
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == '.') g[i][j] = 1;
      if (s[j] == 'S') g[i][j] = 1, sx = i, sy = j;
    }
  }
  cout << bfs();
  return 0;
}
