#include <bits/stdc++.h>
using namespace std;
int n, m, k, mark[1005][1005], basx, basy,
    a[2][4] = {{1, 0, -1, 0}, {0, 1, 0, -1}}, mini = 1000000;
char arr[1005][1005];
map<pair<pair<int, int>, int>, int> mapx;
void fun(int y, int x) {
  mini = min(mini, y / k + (y % k > 0));
  mini = min(mini, x / k + (x % k > 0));
  mini = min(mini, (m - y) / k + ((m - y) % k > 0));
  mini = min(mini, (n - x) / k + ((n - x) % k > 0));
}
void dfs(int y, int x, int kalan) {
  if (mark[y][x] == 1 || kalan < 0) return;
  fun(y, x);
  mark[y][x] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cout << mark[i][j] << " ";
    }
    cout << "\n";
  };
  for (int j = 0; j < 4; j++) {
    if (arr[y + a[0][j]][x + a[1][j]] == '.') {
      dfs(y + a[0][j], x + a[1][j], kalan - 1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 'S') {
        basx = j;
        basy = i;
      }
    }
  }
  dfs(basy, basx, k);
  fun(basy, basx);
  cout << mini;
}
