program prob3
    implicit none
    character(len=1), dimension(:, :), allocatable :: S
    integer :: H, W, i, j, k, tmp, x, y
    integer, dimension(8) :: dx, dy
    dx = (/1, 1, 1, 0, 0,-1,-1,-1/)
    dy = (/1, 0,-1, 1,-1, 1, 0,-1/)
    
    read(*,*) H, W
    allocate(S(H, W))
    do i = 1, H
        do j = 1, W
            read(*,'(a)',advance='no') S(i, j)
        end do
        read(*,*) 
    end do

    do i = 1, H
        do j = 1, W
            if(S(i, j) == '.') then
                tmp = 0

                do k = 1, 8
                    y = i + dy(k)
                    x = j + dx(k)
                    if(y > H .or. y < 1) then
                        cycle
                    end if
                    if(x > W .or. x < 1) then
                        cycle
                    end if

                    if(S(y, x) == '#')then
                        tmp = tmp + 1
                    end if

                end do
                S(i, j) = char(ichar('0') + tmp)
            end if
        end do
    end do
    
    do i = 1, H
        write(*,*) S(i, :)
    end do

    stop
contains
end program prob3