using System;
using System.Collections.Generic;
class TEST
{
    static int height;
    static int width;
    static char[,] field;
    public static void Main()
    {
        string[] str = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
        height = int.Parse(str[0]);
        width = int.Parse(str[1]);

        //  (int x, int y) pointer = (0, 0);

        field = new char[width, height];
        string[,] field_output = new string[width, height];

        for (int i = height-1; i >=0 ; i--)
        {
            int count = 0;
            foreach (char value in Console.ReadLine())
            {
                field[count, i] = value;
                count++;
            }
        }

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                field_output[x, y] = NumOfBombs(x, y);
            }
        }


        for (int y = height-1; y >=0; y--)
        {
            for (int x = 0; x < width; x++)
            {
                Console.Write(field_output[x, y]);
            }
            Console.Write("\r\n");
        }

   //     Console.ReadKey();
    }

    static string NumOfBombs(int x, int y)
    {
        if (field[x, y] == '#')
            return "#";

        int bombcount = 0;
        int xs;
        int ys;

        for (int i = 0; i < 8; i++)
        {
            switch (i)
            {
                case 0:
                    xs = 1;
                    ys = 0;

                    if (IsValidPos(x, y, xs, ys))
                    {
                        if (field[x + xs, y + ys] == '#')
                        {
                            bombcount++;
                        }
                    }
                    break;
                case 1:
                    xs = 1;
                    ys = -1;

                    if (IsValidPos(x, y, xs, ys))
                    {
                        if (field[x + xs, y + ys] == '#')
                        {
                            bombcount++;
                        }
                    }
                    break;
                case 2:
                    xs = 0;
                    ys = -1;

                    if (IsValidPos(x, y, xs, ys))
                    {
                        if (field[x + xs, y + ys] == '#')
                        {
                            bombcount++;
                        }
                    }
                    break;
                case 3:
                    xs = -1;
                    ys = -1;

                    if (IsValidPos(x, y, xs, ys))
                    {
                        if (field[x + xs, y + ys] == '#')
                        {
                            bombcount++;
                        }
                    }
                    break;
                case 4:
                    xs = -1;
                    ys = 0;

                    if (IsValidPos(x, y, xs, ys))
                    {
                        if (field[x + xs, y + ys] == '#')
                        {
                            bombcount++;
                        }
                    }
                    break;
                case 5:
                    xs = -1;
                    ys = 1;

                    if (IsValidPos(x, y, xs, ys))
                    {
                        if (field[x + xs, y + ys] == '#')
                        {
                            bombcount++;
                        }
                    }
                    break;
                case 6:
                    xs = 0;
                    ys = 1;

                    if (IsValidPos(x, y, xs, ys))
                    {
                        if (field[x + xs, y + ys] == '#')
                        {
                            bombcount++;
                        }
                    }
                    break;
                case 7:
                    xs = 1;
                    ys = 1;

                    if (IsValidPos(x, y, xs, ys))
                    {
                        if (field[x + xs, y + ys] == '#')
                        {
                            bombcount++;
                        }
                    }
                    break;
            }
        }


        return bombcount.ToString();
    }

    static bool IsValidPos(int nowx, int nowy, int x, int y)
    {
        if (nowx + x >= 0 && nowx + x < width)
        {
            if (nowy + y >= 0 && nowy + y < height)
            {
                return true;
            }
        }
        return false;
    }

}

Measured memory_bytes: 27512