// C: Grid Repeating 2
"use strict";

const DEBUG_MODE = false;
const rawInput = DEBUG_MODE
  ? "50 50\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "..................................................\n" +
    "................................................##"
  : require("fs")
      .readFileSync("/dev/stdin", "utf8")
      .trim();

const BLACK_SQUARE = "#";

const hasAdjacent = (grid, row, col, maxRow, maxCol) =>
  (row + 1 <= maxRow && grid[row + 1][col] === BLACK_SQUARE) ||
  (col + 1 <= maxCol && grid[row][col + 1] === BLACK_SQUARE) ||
  (row !== 0 && grid[row - 1][col] === BLACK_SQUARE) ||
  (col !== 0 && grid[row][col - 1] === BLACK_SQUARE);

const process = inputRaw => {
  const input = inputRaw.split("\n");
  const gridSize = input[0].split(" ").map(item => parseInt(item));
  const gridHeight = gridSize[0];
  const gridWidth = gridSize[1];
  let grid = [];

  for (let i = 1; i <= gridHeight; i++) {
    const square = input[i];

    grid.push(square.split(""));
  }

  // can use two pointers from start and end to speed this up by half the time

  for (let row = 0; row < gridWidth; row++) {
    for (let col = 0; col < gridHeight; col++) {
      try {
        if (
          grid[row][col] === BLACK_SQUARE &&
          !hasAdjacent(grid, row, col, gridWidth - 1, gridHeight - 1)
        ) {
          return "No";
        }
      } catch (e) {
        // debug runtime error
        return "Yes";
      }
    }
  }

  return "Yes";
};

console.log(process(rawInput));


Measured memory_bytes: 23200