// Template from https://techracho.bpsinc.jp/jhonda/2019_08_05/78537

// input macro for competive-programming from https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, bytes) => {
        read_value!($iter, String).into_bytes()
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();

    println!("{}", solve(&s));
}

macro_rules! test {
    ($($input:expr => $output:expr),* $(,)*) => {
        #[test]
        fn solve_test() {
            $(
                assert_eq!(solve($input), $output);
            )*
        }
    };
}

test! {
r#"3 5
.....
.#.#.
....."# => r#"11211
1#2#1
11211"#,
}

fn solve(src: &str) -> String {
    input! {
        source = src,
        h: usize, w: usize,
        mut s: [chars; h],
    }
    let mut ans = "".to_string();

    for y in 0..h {
        for x in 0..w {
            let mut num = 0;

            if s[y][x] != '#' {
                if 0 < y && 0 < x && s[y - 1][x - 1] == '#' {
                    num += 1;
                }
                if 0 < y && s[y - 1][x] == '#' {
                    num += 1;
                }
                if 0 < y && x < w - 1 && s[y - 1][x + 1] == '#' {
                    num += 1;
                }

                if 0 < x && s[y][x - 1] == '#' {
                    num += 1;
                }
                if x < w - 1 && s[y][x + 1] == '#' {
                    num += 1;
                }

                if y < h - 1 && 0 < x && s[y + 1][x - 1] == '#' {
                    num += 1;
                }
                if y < h - 1 && s[y + 1][x] == '#' {
                    num += 1;
                }
                if y < h - 1 && x < w - 1 && s[y + 1][x + 1] == '#' {
                    num += 1;
                }

                s[y][x] = (num as u8 + '1' as u8 - 1) as char;
            }

            ans.push_str(&s[y][x].to_string());
        }
        if y != h - 1 {
            ans.push_str(&"\n");
        }
    }

    ans.to_string()
}

Measured memory_bytes: 4352