#include <bits/stdc++.h>
using namespace std;
bool vis[803][803];
int dis[803][803];
string s[803];
queue<pair<int, int> > q;
int n, m, k;
int dirx[4] = {0, 0, 1, -1};
int diry[4] = {1, -1, 0, 0};
bool isValid(int x, int y) {
  if (x < n && x >= 0 && y < m && y >= 0) {
    return true;
  }
  return false;
}
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < s[i].size(); j++) {
      if (s[i][j] == 'S') {
        q.push(make_pair(i, j));
        vis[i][j] = 1;
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> tmp = q.front();
    q.pop();
    int curx = tmp.first;
    int cury = tmp.second;
    for (int i = 0; i < 4; i++) {
      int newx = curx + dirx[i];
      int newy = cury + diry[i];
      if (isValid(newx, newy) && s[newx][newy] != '#') {
        if (!vis[newx][newy]) {
          dis[newx][newy] = dis[curx][cury] + 1;
          vis[newx][newy] = true;
          q.push(make_pair(newx, newy));
        }
      }
    }
  }
  int ans = 1e8;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] != '#' && vis[i][j] && dis[i][j] <= k) {
        int mn = i;
        mn = min(mn, n - i);
        mn = min(mn, j);
        mn = min(mn, m - j);
        ans = min(ans, ((mn + k - 1) / k) + 1);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
