const st  = Date.now()

import { readFileSync } from 'fs'

export function minesweeper () {
  const input: string = readFileSync("/dev/stdin", "utf8")
  const inputLine: string[] = input.split('\n').map(l => l.trim())

  let firstLine = inputLine[0]
  const HEIGHT = parseInt(firstLine.split(' ')[0])
  const WIDTH = parseInt(firstLine.split(' ')[1])

  const inputTable =  inputLine.slice(1).map(v => v.split(''))

  const row: string[] = new Array(WIDTH)
  const table: string[][] = new Array(HEIGHT)
  // '0'埋め
  row.fill('0')
  for (let i = 0; i < table.length; i++ ) {
    table[i] = [...row]
  }

  for (let h = 0; h < HEIGHT; h++) {
    for (let w = 0; w < WIDTH; w++) {
      if (inputTable[h][w] === '#') {
        table[h][w] = '#'
        if (h === 0 && h + 1 === HEIGHT) {
          // 上下端の場合
          if (w === 0) {
            // 左端の場合
            if (table[h][w + 1] !== '#') table[h][w + 1] = (parseInt(table[h][w + 1]) + 1).toString()
          } else if (w + 1 === WIDTH) {
            // 右端の場合
            if (table[h][w - 1] !== '#') table[h][w - 1] = (parseInt(table[h][w - 1]) + 1).toString()
          } else {
            if (table[h][w + 1] !== '#') table[h][w + 1] = (parseInt(table[h][w + 1]) + 1).toString()
            if (table[h][w - 1] !== '#') table[h][w - 1] = (parseInt(table[h][w - 1]) + 1).toString()
          }
          continue
        } else if (h === 0) {
          // 上端の場合
          if (w === 0 && w + 1 === WIDTH) {
            if (table[h + 1][w] !== '#') table[h + 1][w] = (parseInt(table[h + 1][w]) + 1).toString()
          } else if (w === 0) {
            // 左上端の場合
            if (table[h + 1][w] !== '#') table[h + 1][w] = (parseInt(table[h + 1][w]) + 1).toString()
            if (table[h][w + 1] !== '#') table[h][w + 1] = (parseInt(table[h][w + 1]) + 1).toString()
            if (table[h + 1][w + 1] !== '#') table[h + 1][w + 1] = (parseInt(table[h + 1][w + 1]) + 1).toString()
          } else if (w + 1 === WIDTH) {
            // 右上端の場合
            if (table[h + 1][w] !== '#') table[h + 1][w] = (parseInt(table[h + 1][w]) + 1).toString()
            if (table[h][w - 1] !== '#') table[h][w - 1] = (parseInt(table[h][w - 1]) + 1).toString()
            if (table[h + 1][w - 1] !== '#') table[h + 1][w - 1] = (parseInt(table[h + 1][w - 1]) + 1).toString()
          } else {
            // 上端の場合
            if (table[h + 1][w] !== '#') table[h + 1][w] = (parseInt(table[h + 1][w]) + 1).toString()
            if (table[h][w + 1] !== '#') table[h][w + 1] = (parseInt(table[h][w + 1]) + 1).toString()
            if (table[h + 1][w + 1] !== '#') table[h + 1][w + 1] = (parseInt(table[h + 1][w + 1]) + 1).toString()
            if (table[h][w - 1] !== '#') table[h][w - 1] = (parseInt(table[h][w - 1]) + 1).toString()
            if (table[h + 1][w - 1] !== '#') table[h + 1][w - 1] = (parseInt(table[h + 1][w - 1]) + 1).toString()
          }
        } else if (h + 1 === HEIGHT) {
          // 下端の場合
          if (w === 0 && w + 1 === WIDTH) {
            if (table[h - 1][w] !== '#') table[h - 1][w] = (parseInt(table[h - 1][w]) + 1).toString()
          } else if (w === 0) {
            // 左下端の場合
            if (table[h - 1][w] !== '#') table[h - 1][w] = (parseInt(table[h - 1][w]) + 1).toString()
            if (table[h][w + 1] !== '#') table[h][w + 1] = (parseInt(table[h][w + 1]) + 1).toString()
            if (table[h - 1][w + 1] !== '#') table[h - 1][w + 1] = (parseInt(table[h - 1][w + 1]) + 1).toString()
          } else if (w + 1 === WIDTH) {
            // 右下端の場合
            if (table[h - 1][w] !== '#') table[h - 1][w] = (parseInt(table[h - 1][w]) + 1).toString()
            if (table[h][w - 1] !== '#') table[h][w - 1] = (parseInt(table[h][w - 1]) + 1).toString()
            if (table[h - 1][w - 1] !== '#') table[h - 1][w - 1] = (parseInt(table[h - 1][w - 1]) + 1).toString()
          } else {
            // 下端の場合
            if (table[h - 1][w] !== '#') table[h - 1][w] = (parseInt(table[h - 1][w]) + 1).toString()
            if (table[h][w + 1] !== '#') table[h][w + 1] = (parseInt(table[h][w + 1]) + 1).toString()
            if (table[h - 1][w + 1] !== '#') table[h - 1][w + 1] = (parseInt(table[h - 1][w + 1]) + 1).toString()
            if (table[h][w - 1] !== '#') table[h][w - 1] = (parseInt(table[h][w - 1]) + 1).toString()
            if (table[h - 1][w - 1] !== '#') table[h - 1][w - 1] = (parseInt(table[h - 1][w - 1]) + 1).toString()
          }
        } else {
          if (w === 0 && w + 1 === WIDTH) {
            if (table[h + 1][w] !== '#') table[h + 1][w] = (Number(table[h + 1][w]) + 1).toString()
            if (table[h - 1][w] !== '#') table[h - 1][w] = (parseInt(table[h - 1][w]) + 1).toString()
          } else if (w === 0) {
            // 左端の場合
            if (table[h + 1][w] !== '#') table[h + 1][w] = (parseInt(table[h + 1][w]) + 1).toString()
            if (table[h][w + 1] !== '#') table[h][w + 1] = (parseInt(table[h][w + 1]) + 1).toString()
            if (table[h + 1][w + 1] !== '#') table[h + 1][w + 1] = (parseInt(table[h + 1][w + 1]) + 1).toString()
            if (table[h - 1][w] !== '#') table[h - 1][w] = (parseInt(table[h - 1][w]) + 1).toString()
            if (table[h - 1][w + 1] !== '#') table[h - 1][w + 1] = (parseInt(table[h - 1][w + 1]) + 1).toString()
          } else if (w + 1 === WIDTH) {
            // 右端の場合
            if (table[h + 1][w] !== '#') table[h + 1][w] = (parseInt(table[h + 1][w]) + 1).toString()
            if (table[h][w - 1] !== '#') table[h][w - 1] = (parseInt(table[h][w - 1]) + 1).toString()
            if (table[h + 1][w - 1] !== '#') table[h + 1][w - 1] = (parseInt(table[h + 1][w - 1]) + 1).toString()
            if (table[h - 1][w] !== '#') table[h - 1][w] = (parseInt(table[h - 1][w]) + 1).toString()
            if (table[h - 1][w - 1] !== '#') table[h - 1][w - 1] = (parseInt(table[h - 1][w - 1]) + 1).toString()
          } else {
            // それ以外
            if (table[h + 1][w] !== '#') table[h + 1][w] = (Number(table[h + 1][w]) + 1).toString()
            if (table[h][w + 1] !== '#') table[h][w + 1] = (parseInt(table[h][w + 1]) + 1).toString()
            if (table[h + 1][w + 1] !== '#') table[h + 1][w + 1] = (parseInt(table[h + 1][w + 1]) + 1).toString()
            if (table[h - 1][w] !== '#') table[h - 1][w] = (parseInt(table[h - 1][w]) + 1).toString()
            if (table[h - 1][w + 1] !== '#') table[h - 1][w + 1] = (parseInt(table[h - 1][w + 1]) + 1).toString()
            if (table[h][w - 1] !== '#') table[h][w - 1] = (parseInt(table[h][w - 1]) + 1).toString()
            if (table[h + 1][w - 1] !== '#') table[h + 1][w - 1] = (parseInt(table[h + 1][w - 1]) + 1).toString()
            if (table[h - 1][w - 1] !== '#') table[h - 1][w - 1] = (parseInt(table[h - 1][w - 1]) + 1).toString()
          }
        }
      }
    }
  }
  const rowList: string[] = []
  const result: string = table.map(r => r.join('')).join('\n')
  console.log(result)
}

minesweeper()

Measured memory_bytes: 15332