program prob3
    implicit none
    integer::H, W, i,j
    character,allocatable::a(:,:),c(:,:)
    character(len=50)::S
    integer,allocatable::ans(:,:)
    read(*,*) H, W
    allocate(a(H+2,W+2),c(H+2,W+2))
    allocate(ans(H+2,W+2))
    a(1,:) = "."
    a(H+2,:) = "."
    a(:,1) = '.'
    a(:,W+2) = '.'
    do i = 1, H
        read(*,*) S
        do j = 1, W
            a(i+1,j+1) = S(j:j)
        end do
    end do

    ans = 0
    do i = 2, H+1
        do j = 2, W+1
            if(a(i,j) .eq. "#") then
                c(i,j) = "#"
            else
                if(a(i-1,j-1) .eq. "#") then
                    ans(i,j) = ans(i,j) + 1 
                end if
                if(a(i,j-1) .eq. "#") then
                    ans(i,j) = ans(i,j) + 1
                end if
                if(a(i+1,j-1) .eq. "#") then
                    ans(i,j) = ans(i,j) + 1
                end if
                if(a(i-1,j) .eq. "#") then
                    ans(i,j) = ans(i,j) + 1
                end if
                if(a(i+1,j) .eq. "#")then
                    ans(i,j) = ans(i,j) + 1
                end if
                if(a(i-1,j+1) .eq. "#") then
                    ans(i,j) = ans(i,j) + 1
                end if
                if(a(i,j+1) .eq. "#")   then
                    ans(i,j) = ans(i,j) + 1
                end if
                if(a(i+1,j+1) .eq. "#")then
                    ans(i,j) = ans(i,j) + 1
                end if
            end if
        end do
    end do

    do i = 2, H+1
        do j= 2, W+1
            if(ans(i,j) == 0)then
                if(c(i,j) .ne. "#")then
                    c(i,j) = '0'
                end if
            else
                c(i,j) = char(ichar('0')+ ans(i,j))
            end if
        end do
    end do

    do i = 2, H+1
        do j = 2, W+1
            S(j-1:j-1) = c(i,j)
        end do
        write(*,*) S
    end do

    deallocate(a)
    stop
end program