#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <class T, class U>
void amin(T& x, U y) {
  if (x > y) x = y;
}
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
bool in_range(ll x, ll y, ll H, ll W) {
  return 0 <= x && x < H && 0 <= y && y < W;
}
vector<vector<int>> shortest_path(const vector<vector<char>>& grid, char wall,
                                  char start) {
  int H = grid.size();
  int W = grid[0].size();
  vector<vector<int>> dp(H, vector<int>(W, H * W));
  queue<pii> q;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (grid[i][j] == start) {
        dp[i][j] = 0;
        q.push(pii(i, j));
      }
    }
  }
  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();
    for (int k = 0; k < 4; ++k) {
      int x_next = x + dx[k];
      int y_next = y + dy[k];
      if (in_range(x_next, y_next, H, W) && grid[x_next][y_next] != wall &&
          dp[x_next][y_next] > dp[x][y] + 1) {
        dp[x_next][y_next] = dp[x][y] + 1;
        q.push(pii(x_next, y_next));
      }
    }
  }
  return dp;
}
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<char>> A(H, vector<char>(W));
  for (int i = 0; i < (H); ++i)
    for (int j = 0; j < (W); ++j) cin >> A[i][j];
  auto dp = shortest_path(A, '#', 'S');
  for (int i = 0; i < (H); ++i)
    for (int j = 0; j < (W); ++j) {
      if (dp[i][j] <= K) {
        A[i][j] = 'S';
      } else {
        A[i][j] = '.';
      }
    }
  auto dp2 = shortest_path(A, '#', 'S');
  int result = H * W;
  for (int i = 0; i < (H); ++i) {
    amin(result, (dp2[i][0] + K - 1) / K);
    amin(result, (dp2[i][W - 1] + K - 1) / K);
  }
  for (int j = 0; j < (W); ++j) {
    amin(result, (dp2[0][j] + K - 1) / K);
    amin(result, (dp2[H - 1][j] + K - 1) / K);
  }
  ++result;
  cout << result << endl;
  return 0;
}
