Scanf.scanf "%d %d %d" (fun h w k ->
    let s = Array.init h (fun _ -> Scanf.scanf " %s" (fun s -> s)) in
    let r = Array.init h (fun i ->
        let rec loop j acc =
            if j = w then acc else
                loop (j + 1) (if s.(i).[j] = '#' then acc + 1 else acc)
        in
        loop 0 0
    ) in
    let result = Array.make_matrix h w 0 in
    let start =
        let rec loop i =
            if r.(i) > 0 then i else loop (i + 1)
        in
        loop 0
    in
    let rec loop_y y cur =
        if y < h then (
            if r.(y) = 0 then (
                result.(y) <- Array.copy result.(y - 1);
                loop_y (y + 1) cur
            ) else (
                let rec loop_x x mode cur =
                    if x = w then loop_y (y + 1) cur else
                        if mode = 0 then (
                            result.(y).(x) <- cur;
                            let mode = if s.(y).[x] = '.' then mode else mode + 1 in
                            loop_x (x + 1) mode cur
                        ) else (
                            let cur = if s.(y).[x] = '.' then cur else cur + 1 in
                            result.(y).(x) <- cur;
                            loop_x (x + 1) mode cur
                        )
                in
                loop_x 0 0 (cur + 1)
            )
        )
    in
    loop_y start 0;
    for y = 0 to start - 1 do
        for x = 0 to w - 1 do result.(y).(x) <- result.(start).(x) done
    done;
    Array.iter (fun arr -> Array.iter (Printf.printf "%d ") arr; print_newline ()) result
)