using System;
using System.Collections.Generic;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("3 5");
            WillReturn.Add(".....");
            WillReturn.Add(".#.#.");
            WillReturn.Add(".....");
            //11211
            //1#2#1
            //11211
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 5");
            WillReturn.Add("#####");
            WillReturn.Add("#####");
            WillReturn.Add("#####");
            //#####
            //#####
            //#####
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("6 6");
            WillReturn.Add("#####.");
            WillReturn.Add("#.#.##");
            WillReturn.Add("####.#");
            WillReturn.Add(".#..#.");
            WillReturn.Add("#.##..");
            WillReturn.Add("#.#...");
            //#####3
            //#8#7##
            //####5#
            //4#65#2
            //#5##21
            //#4#310
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int UB_X;
    static int UB_Y;

    static void Main()
    {
        List<string> InputList = GetInputList();

        UB_X = InputList[1].Length - 1;
        UB_Y = InputList.Count - 2;

        char[,] BanArr = new char[UB_X + 1, UB_Y + 1];
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                BanArr[LoopX, LoopY] = InputList[LoopY + 1][LoopX];
            }
        }

        Func<int, int, bool> IsMineFunc = (pX, pY) =>
        {
            if (pX < 0 || UB_X < pX) return false;
            if (pY < 0 || UB_Y < pY) return false;
            return BanArr[pX, pY] == '#';
        };

        char[,] AnswerArr = new char[UB_X + 1, UB_Y + 1];

        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (BanArr[LoopX, LoopY] == '#') {
                    AnswerArr[LoopX, LoopY] = '#';
                    continue;
                }
                int MineCnt = 0;
                if (IsMineFunc(LoopX - 1, LoopY - 1)) MineCnt++;
                if (IsMineFunc(LoopX - 1, LoopY)) MineCnt++;
                if (IsMineFunc(LoopX - 1, LoopY + 1)) MineCnt++;
                if (IsMineFunc(LoopX, LoopY - 1)) MineCnt++;
                if (IsMineFunc(LoopX, LoopY + 1)) MineCnt++;
                if (IsMineFunc(LoopX + 1, LoopY - 1)) MineCnt++;
                if (IsMineFunc(LoopX + 1, LoopY)) MineCnt++;
                if (IsMineFunc(LoopX + 1, LoopY + 1)) MineCnt++;
                AnswerArr[LoopX, LoopY] = (char)('0' + MineCnt);
            }
        }
        PrintBan(AnswerArr);
    }

    static void PrintBan(char[,] pBanArr)
    {
        var sb = new System.Text.StringBuilder();

        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                sb.Append(pBanArr[X, Y]);
            }
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }
}


Measured memory_bytes: 13140