#include <bits/stdc++.h>
using namespace std;
char dt[880][880];
int dis[880][880];
int n, m, k;
inline bool pc(int first, int second) {
  if (first == n || second == m || first < 0 || second < 0 ||
      dt[first][second] == '#' || dis[first][second] != -1)
    return 0;
  return 1;
}
queue<pair<int, int> > q;
int fx[4] = {1, -1, 0, 0}, fy[4] = {0, 0, 1, -1};
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) scanf("%s", dt[i]);
  memset(dis, -1, sizeof(dis));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      if (dt[i][j] == 'S') q.push(pair<int, int>(i, j)), dis[i][j] = 0;
    ;
  }
  int ans = 1008610086;
  while (!q.empty()) {
    pair<int, int> st = q.front();
    q.pop();
    ans = min(min(st.first, st.second),
              min(min(n - st.first, m - st.second) - 1, ans));
    for (int i = 0; i < 4; i++) {
      pair<int, int> to = pair<int, int>(st.first + fx[i], st.second + fy[i]);
      if (pc(to.first, to.second)) {
        q.push(to);
        dis[to.first][to.second] = 0;
      }
    }
  }
  printf("%d\n", (ans + k - 1) / k + 1);
  return 0;
}
