Scanf.sscanf (read_line ()) "%d %d" (fun h w ->
    let s = Array.init h (fun _ -> Bytes.of_string @@ read_line ()) in
    for y = 0 to h - 1 do
        for x = 0 to w - 1 do
            if Bytes.get s.(y) x = '.' then (
                let acc = 0 in
                let acc = if y > 0     && x > 0     && Bytes.get s.(y - 1) (x - 1) = '#' then acc + 1 else acc in
                let acc = if y > 0                  && Bytes.get s.(y - 1) (x)     = '#' then acc + 1 else acc in
                let acc = if y > 0     && x < w - 1 && Bytes.get s.(y - 1) (x + 1) = '#' then acc + 1 else acc in
                let acc = if              x > 0     && Bytes.get s.(y)     (x - 1) = '#' then acc + 1 else acc in
                let acc = if              x < w - 1 && Bytes.get s.(y)     (x + 1) = '#' then acc + 1 else acc in
                let acc = if y < h - 1 && x > 0     && Bytes.get s.(y + 1) (x - 1) = '#' then acc + 1 else acc in
                let acc = if y < h - 1              && Bytes.get s.(y + 1) (x)     = '#' then acc + 1 else acc in
                let acc = if y < h - 1 && x < w - 1 && Bytes.get s.(y + 1) (x + 1) = '#' then acc + 1 else acc in
                Bytes.set s.(y) x (char_of_int (acc + 48))
            )
        done
    done;
    for y = 0 to h - 1 do
        print_endline @@ Bytes.to_string s.(y)
    done
)