#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int N, M, K;
char A[800][800];
int dist[800][800];
vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};
int main() {
  cin >> N >> M >> K;
  int sx, sy;
  for (int(i) = 0; (i) < (N); (i)++)
    for (int(j) = 0; (j) < (M); (j)++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') {
        sx = i;
        sy = j;
        A[i][j] = '.';
      }
    }
  for (int(i) = 0; (i) < (N); (i)++)
    for (int(j) = 0; (j) < (M); (j)++) dist[i][j] = INF;
  queue<pair<int, int> > q;
  q.push(make_pair(sx, sy));
  dist[sx][sy] = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int x = p.first;
    int y = p.second;
    for (int(k) = 0; (k) < (4); (k)++) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (0 <= nx && nx < N && 0 <= ny && ny < M)
        if (A[nx][ny] != '#' && dist[nx][ny] == INF) {
          dist[nx][ny] = dist[x][y] + 1;
          q.push(make_pair(nx, ny));
        }
    }
  }
  for (int(i) = 0; (i) < (N); (i)++) {
    for (int(j) = 0; (j) < (M); (j)++) cout << dist[i][j] << " ";
    cout << endl;
  }
  int step = 1000;
  for (int(i) = 0; (i) < (N); (i)++)
    for (int(j) = 0; (j) < (M); (j)++)
      if (dist[i][j] <= K) {
        int tx = min(i, N - 1 - i);
        int ty = min(j, M - 1 - j);
        int tmp = min(tx, ty);
        step = min(step, tmp);
      }
  int ans;
  if (step % K == 0)
    ans = 1 + step / K;
  else
    ans = 2 + step / K;
  cout << ans << endl;
}
