let () = Scanf.scanf "%d %d %d\n" @@ fun h w k ->
  let ass = Array.init h @@ fun _ -> Scanf.scanf "%s\n" @@ fun as_ -> as_ in
  let sy, sx =
    List.find (fun (y, x) -> ass.(y).[x] = 'S') @@
    List.concat @@ Array.to_list @@ Array.init h @@ fun y ->
      Array.to_list @@ Array.init w @@ fun x -> (y, x) in
  let visited = Array.make_matrix h w false in
  let rec dfs y x = function
    | 0 -> ()
    | k ->
        if
          0 <= y && y < h &&
          0 <= x && x < w &&
          ass.(y).[x] <> '#' && 
          not visited.(y).(x)
        then begin
          visited.(y).(x) <- true;
          dfs (y - 1) x (k - 1);
          dfs y (x - 1) (k - 1);
          dfs (y + 1) x (k - 1);
          dfs y (x + 1) (k - 1)
        end in
  dfs sy sx (k + 1);
  Printf.printf "%d\n" @@ succ @@
  Array.fold_left (Array.fold_left min) max_int @@
  Array.init h @@ fun i ->
    Array.init w @@ fun j ->
      if not visited.(i).(j)
      then max_int
      else (min (min i j) (min (h - 1 - i) (w - 1 - j)) + k - 1) / k
