Problem:


Score: 300 points

Problem Statement

We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).

Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..

However, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.

Determine if square1001 can achieve his objective.  

Constraints

H is an integer between 1 and 50 (inclusive).

W is an integer between 1 and 50 (inclusive).

For every (i, j) (1 \leq i \leq H, 1 \leq j \leq W), s_{i, j} is # or ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}
s_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}
  :   :
s_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}

Output

If square1001 can achieve his objective, print Yes; if he cannot, print No.

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.

Sample Input 2

5 5
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##.

Sample Output 3

Yes



Sample input:
3 3
.#.
###
.#.


Sample output:
Yes

Submission status: Accepted

Solution code:
import math
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

# grid
# N = I()
# 黒く塗りつぶす#が独立していたら不可
H,W = LI()
S = [list(_S()) for _ in range (H)]

def check(x, y):
    # 移動する4方向をループ
    res = False
    # for dx,dy in itertools.product([-1,0,1],repeat=2):
    dxs = [-1,1]
    dys = [-1,1]
    for dx in dxs:
        nx = x + dx
        ny = y
        if 0 <= nx and nx < H and S[nx][ny] == "#":
            res = True
    for dy in dys:
        nx = x
        ny = y + dy
        if 0 <= ny and ny < W and S[nx][ny] == "#":
            res = True
    return res

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            if not check(i,j):
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
H, W=map(int,input().split())
s=[[0]*(W+2) for i in range(H+2)]
for ii in range(1,H+1):
    A=list(input())
    for jj in range(1,W+1):
        if A[jj-1]=='#':
            s[ii][jj]=1
can=1
for ii in range(1,H+1):
    for jj in range(1,W+1):
        if s[ii][jj]==1:
            if s[ii-1][jj]+s[ii+1][jj]+s[ii][jj-1]+s[ii][jj+1]==0:
                can=0
                break
    if can==0:
        break

if can==1:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    H, W = LI()
    s = [SS() for _ in range(H)]

    d = ((1, 0), (0, 1), (-1, 0), (0, -1))

    def dfs(cy, cx, tmp):
        tmp.append((cy, cx))
        visited[cy][cx] = True
        for dy, dx in d:
            ny = cy + dy
            nx = cx + dx
            if 0 <= ny < H and 0 <= nx < W and s[ny][nx] == '#' and not visited[ny][nx]:
                dfs(ny, nx, tmp)

    # 大きさ1の#の島があったらダメ
    is_ok = True
    visited = [[False] * W for _ in range(H)]
    for i, j in itertools.product(range(H), range(W)):
        if s[i][j] == '#' and not visited[i][j]:
            tmp = []
            dfs(i, j, tmp)
            if len(tmp) == 1:
                is_ok = False
                break

    if is_ok:
        print('Yes')
    else:
        print('No')

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
ans = 0
for i in range(H):
  for j in range(W):
    if S[i][j] == "#":
      flag = 0
      if i > 0 and S[i-1][j] == "#":
        flag = 1
      if i < H-1 and S[i+1][j] == "#":
        flag = 1
      if j > 0 and S[i][j-1] == "#":
        flag = 1
      if j < W-1 and S[i][j+1] == "#":
        flag = 1
      if flag == 0:
        ans = 1
print("Yes" if ans == 0 else "No")

Submission status: Accepted

Solution code:
def main():
    H, W = map(int, input().split())
    s = [list(input()) for _ in range(H)]
    dx = [0, -1, 1, 0]
    dy = [-1, 0, 0, 1]
    flag = True

    for i in range(H):
        for j in range(W):
            if s[i][j] == ".":
                continue

            cnt = 0
            for k in range(4):
                x = j + dx[k]
                y = i + dy[k]
                if x >= 0 and x < W and y >= 0 and y < H:
                    if s[y][x] == "#":
                        cnt += 1

            if cnt == 0:
                flag = False

    if flag == True:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

h, w = list(map(int, input().split()))
s = [list(str(input())) for i in range(h)]

for i in range(h):
    for j in range(w):
        tmp = 1
        if s[i][j] == "#":
            tmp = 0
            if i > 0:
                if s[i-1][j] == "#":
                    tmp += 1
            if i < h-1:
                if s[i+1][j] == "#":
                    tmp += 1
            if j > 0:
                if s[i][j-1] == "#":
                    tmp += 1
            if j < w-1:
                if s[i][j+1] == "#":
                    tmp += 1
        if tmp == 0:
            print("No")
            exit()
print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [input().rstrip() for _ in range(h)]

for y in range(h):
    for x in range(w):
        if s[y][x] == '.':
            continue
        if y > 0 and s[y - 1][x] == '#':
            continue
        if y < h - 1 and s[y + 1][x] == '#':
            continue
        if x > 0 and s[y][x - 1] == '#':
            continue
        if x < w - 1 and s[y][x + 1] == '#':
            continue
        print('No')
        exit(0)
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

#   .
# . # .
#   .
# を探す

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            temp = set()
            if i != 0:
                temp.add(S[i-1][j])
            if i != H-1:
                temp.add(S[i+1][j])
            if j != 0:
                temp.add(S[i][j-1])
            if j != W-1:
                temp.add(S[i][j+1])
            if temp == set("."):
                print("No")
                exit()

print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = list(list(input()) for _ in range(H))
ans = True
d = [(0,1),(0,-1),(1,0),(-1,0)]
for y in range(H):
    for x in range(W):
        if s[y][x] == "#":
            for dy, dx in d:
                ny = y + dy
                nx = x + dx
                if ny<0 or H<=ny or nx<0 or W<=nx:
                    continue
                if s[ny][nx]=="#":
                    break
            else:
                ans = False
print("Yes" if ans else "No")

Submission status: Accepted

Solution code:
#
# abc096 c
#
import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 3
.#.
###
.#."""
        output = """Yes"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """5 5
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#"""
        output = """No"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##."""
        output = """Yes"""
        self.assertIO(input, output)


def resolve():
    H, W = map(int, input().split())
    S = [["."] * (W+2) for _ in range(H+2)]
    for i in range(H):
        S[i+1][1:W+1] = list(".") + list(input())

    ans = "Yes"
    for i in range(1, H+2):
        for j in range(1, W+2):
            if S[i][j] == "#":
                if S[i-1][j] == "." and S[i][j-1] == "." and S[i][j+1] == "." and S[i+1][j] == ".":
                    ans = "No"
                    break

    print(ans)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, input().split())
    inlis = list()
    for _ in range(h):
        alis = list(input())
        inlis.append(alis)
    
    for i in range(h):
        for j in range(w):
            if inlis[i][j] == "#":
                tmp = 0
                if i+1 < h:
                    if inlis[i+1][j] != "#":
                        tmp += 1
                else:
                    tmp += 1
                if j+1 < w:
                    if inlis[i][j+1] != "#":
                        tmp += 1
                else:
                    tmp += 1
                if j-1 >= 0:
                    if  inlis[i][j-1] != "#":
                        tmp += 1
                else:
                    tmp += 1
                if i-1 >= 0:
                    if inlis[i-1][j] != "#":
                        tmp += 1
                else:
                    tmp += 1
                       
                if tmp == 4:
                    print("No")
                    exit()
    print('Yes')
        




if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
ans = [[0]*w for _ in range(h)]
def dfs(x,y):
    if 0 <= x < h and 0 <= y < w:
        if s[x][y] == "#":
            ans[x][y] = 1 
            
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            dfs(i+1, j)
            dfs(i, j+1)
            dfs(i, j-1)
            dfs(i-1, j)
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            if ans[i][j]!=1:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
ans = [[0]*w for _ in range(h)]
cnt=0
def dfs(x,y):
    if 0 <= x < h and 0 <= y < w:
        if s[x][y] == "#" and cnt<2 and ans[x][y]==0:
            ans[x][y] = 1 
            

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            dfs(i+1, j)
            dfs(i, j+1)
            dfs(i, j-1)
            dfs(i-1, j)
        cnt=0
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            if ans[i][j]!=1:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
h, w = list(map(int, input().split()))
grid = []
for i in range(h):
    grid.append(input())

for i in range(h):
    for j in range(w):
        if grid[i][j] == ".":
            continue

        check = []
        if j != 0:
            check.append([i, j-1])
        if j != w-1:
            check.append([i, j+1])
        if i != 0:
            check.append([i-1, j])
        if i != h-1:
            check.append([i+1, j])

        p = False
        for c in check:
            if grid[c[0]][c[1]] == "#":
                p = True
                break

        if not p:
            print("No")
            exit()

print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = []
for _ in range(H):
    s.append(input())
dx = (0, 1, 0, -1)
dy = (1, 0, -1, 0)

for i in range(H):
    for j in range(W):
        if s[i][j] == "#":
            for d in range(4):
                ni = i + dy[d]
                nj = j + dx[d]
                if ni < 0 or ni >= H:
                    continue
                if nj < 0 or nj >= W:
                    continue
                if s[ni][nj] == "#":
                    break
            else:
                break
            continue
    else:
        continue
    print("No")
    break
else:
    print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split(' '))
S = []
for i in range(H):
  S.append(input())
 
dj = [-1, 0, 0, 1]
dk = [0, -1, 1, 0]
flug = []
for j in range(H):
  for k in range(W):
    judge = 'No'
    if S[j][k] == '.':
      judge = 'Yes'
    if S[j][k] == '#':
      for d in range(4):
        if 0 <= j+dj[d] <= H-1:
          if 0 <= k+dk[d] <= W-1:
            if S[j+dj[d]][k+dk[d]] == '#':
              judge = 'Yes'
      if judge == 'No':
        flug.append(0)
if 0 in flug:
  print('No')
else:
  print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = []
s.append(["."]*(W+2))
for _ in range(H):
    string = input()
    string = "." + string + "."
    s.append(list(string))
s.append(["."]*(W+2))
ans = True
for row in range(1, H+1):
    for col in range(1, W+1):
        if s[row][col] == "#":
            is_ok = False
            for dh, dw in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                if s[row+dh][col+dw] == "#":
                    is_ok = True
                    break
            if not is_ok:
                ans = False
                break
if ans:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]

d = [-1, 1]
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            ls = []
            for i in range(2):
                for j in range(2):
                    hs = h + d[i]
                    ws = w + d[j]
                    if 0 <= hs < H and 0 <= ws < W:
                        ls.append(S[hs][w])
                        ls.append(S[h][ws])
            if '#' not in ls:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = []
for h in range(H):
    S_h = list(input())
    S.append(S_h)

d = [-1, 1]
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            ls = []
            for i in range(2):
                for j in range(2):
                    hs = h + d[i]
                    ws = w + d[j]
                    if 0 <= hs < H and 0 <= ws < W:
                        ls.append(S[hs][w])
                        ls.append(S[h][ws])
            if '#' not in ls:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
lst=[]
lst.append('.'*(w+2))
for i in range(h):
  lst.append('.'+input()+'.')
lst.append('.'*(w+2))

dx=[1,-1,0,0]
dy=[0,0,1,-1]

for i in range(1,h+1):
  for j in range(1,w+1):
    if lst[i][j]=='#':
      judge=False
      for k in range(4):
        nx=i+dx[k]
        ny=j+dy[k]
        if lst[nx][ny]=='#':
          judge=True
          
      if judge==False:
        print('No')
        exit()
        
print('Yes')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[input() for _ in range(H)]

D=[(0,1),(0,-1),(1,0),(-1,0)]

ans=True
for h in range(H):
    for w in range(W):
        if (S[h][w]=='#'):
            f=False
            for dh,dw in D:
                h2=h+dh
                w2=w+dw
                if h2<0 or H<=h2 or w2<0 or W<=w2: continue
                if S[h2][w2]=='#':
                    f=True
                    break
            else:
                ans=False
print('Yes' if ans else 'No')

Submission status: Accepted

Solution code:
import sys
h, w = list(map(int, input().split()))
maze = []
for i in range(h):
    maze.append(input())

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
for i in range(h):
    for j in range(w):
        if maze[i][j] == '#':
            flag = False
            for k in range(4):
                nx = i + dx[k]
                ny = j + dy[k]
                if nx >= 0 and nx < h and ny >= 0 and ny < w and maze[nx][ny] == '#':
                    flag = True
                    break
            if not flag:
                print('No')
                sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = ['.'+input()+'.' for _ in range(H)]
s.insert(0,'.'*(W+2))
s.append('.'*(W+2))
for i in range(1,H+1):
    for j in range(1,W+1):
        if s[i][j] == '#' and (s[i-1][j] + s[i][j-1:j+2] + s[i+1][j]).count('#') == 1:
            print('No')
            exit()
print('Yes')

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
gazo = []
for i in range(h):
    s = str(input())
    s_i = []
    s_i.append(s[0])
    for j in range(1,w):
        if s[j] == '.':
            s_i.append(s[j])
        else:
            if s[j-1]=='#' and s[j]=='#':
                s_i[j-1]='-'
                s_i.append('-')
            elif s[j-1]=='-' and s[j]=='#':
                s_i.append('-')
            else:
                s_i.append(s[j])
    gazo.append(s_i)

for j in range(0,w):
    for i in range(1,h):
        if gazo[i-1][j]=='#' and gazo[i][j]=='#':
            gazo[i-1][j]='-'
            gazo[i][j]='-'
        elif gazo[i-1][j]=='-' and gazo[i][j]=='#':
            gazo[i][j]='-'
        elif gazo[i-1][j]=='#' and gazo[i][j]=='-':
            gazo[i-1][j]='-'
#for i in range(h):
#    print(gazo[i])
isError = False
for i in range(h):
    for j in range(w):
        if gazo[i][j]=='#':
            isError = True
            break

if isError:
    print('No')
else:
    print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [list(input()) for i in range(H)]

fail = 0

for i in range(H):
  for j in range(W):
    if i == 0:
      if j == 0:
        if s[i][j] == '.':
          pass
        elif s[i][min(W-1,j+1)] == '#' or s[min(H-1,i+1)][j] == '#':
          pass
        else:
          fail += 1
      elif j == W - 1:
        if s[i][j] == '.':
          pass
        elif s[i][max(0,j-1)] == '#' or s[min(H-1,i+1)][j] == '#':
          pass
        else:
          fail += 1
      else:
        if s[i][j] == '.':
          pass
        elif s[i][j-1] == '#' or s[i][j+1] == '#' or s[min(H-1,i+1)][j] == '#':
          pass
        else:
          fail += 1
    elif i == H - 1:
      if j == 0:
        if s[i][j] == '.':
          pass
        elif s[i][min(W-1,j+1)] == '#' or s[max(0,i-1)][j] == '#':
          pass
        else:
          fail += 1
      elif j == W - 1:
        if s[i][j] == '.':
          pass
        elif s[i][max(0,j-1)] == '#' or s[max(0,i-1)][j] == '#':
          pass
        else:
          fail += 1
      else:
        if s[i][j] == '.':
          pass
        elif s[i][j-1] == '#' or s[i][j+1] == '#' or s[max(0,i-1)][j] == '#':
          pass
        else:
          fail += 1
    else:
      if j == 0:
        if s[i][j] == '.':
          pass
        elif s[i-1][j] == '#' or s[i+1][j] == '#' or s[i][min(H-1,j+1)] == '#':
          pass
        else:
          fail += 1
      elif j == W - 1:
        if s[i][j] == '.':
          pass
        elif s[i-1][j] == '#' or s[i+1][j] == '#' or s[i][max(0,j-1)] == '#':
          pass
        else:
          fail += 1
      else:
        if s[i][j] == '.':
          pass
        elif s[i-1][j] == '#' or s[i+1][j] == '#' or s[i][j-1] == '#' or s[i][j+1] == '#':
          pass
        else:
          fail += 1
          
isAllWhite = False
count = 0
for i in range(H):
  for j in range(W):
    if s[i][j] == '.':
      count += 1
else:
  if count == H * W:
    isAllWhite = True
    
  if isAllWhite or fail == 0:
    print('Yes')
  else:
    print('No')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[0]*h
for i in range(h):
    s[i] = input()

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            if i != 0:
                if s[i-1][j] == '#':
                    continue
            if j != 0:
                if s[i][j-1] == '#':
                    continue
            if i != h-1:
                if s[i+1][j] == '#':
                    continue
            if j != w-1:
                if s[i][j+1] == '#':
                    continue
        else:
            continue
        print('No')
        exit()

print('Yes')

Submission status: Accepted

Solution code:
LI = lambda: list(map(int, input().split()))

H, W = LI()
S = [input() for _ in range(H)]


def main():
    for i in range(H):
        for j in range(W):
            if S[i][j] == ".":
                continue
            if j > 0 and S[i][j - 1] == "#":
                continue
            if j < W - 1 and S[i][j + 1] == "#":
                continue
            if i > 0 and S[i - 1][j] == "#":
                continue
            if i < H - 1 and S[i + 1][j] == "#":
                continue
            print("No")
            return
    print("Yes")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
zero_pad = '.' * (W + 2)
s = []
s.append(zero_pad)
for i in range(H):
    s.append('.' + input() + '.')
s.append(zero_pad)

for h in range(1, H + 1):
    for w in range(1, W + 1):
        if s[h][w] == '#':
            if s[h - 1][w] == '.' and s[h + 1][w] == '.' and s[h][w - 1] == '.' and s[h][w + 1] == '.':
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [input() for i in range(H)]

# 四隅
# 左上
if s[0][0] == '#':
    if s[0][1] == '.' and s[1][0] == '.':
        print('No')
        exit()
# 右上
if s[0][W - 1] == '#':
    if s[0][W - 2] == '.' and s[1][W - 1] == '.':
        print('No')
        exit()
# 左下
if s[H - 1][0] == '#':
    if s[H - 1][1] == '.' and s[H - 2][0] == '.':
        print('No')
        exit()
# 右下
if s[H - 1][W - 1] == '#':
    if s[H - 1][W - 2] == '.' and s[H - 2][W - 1] == '.':
        print('No')
        exit()

# 上辺
for i in range(1, W - 1):
    if s[0][i] == '#':
        if s[0][i - 1] == '.' and s[0][i + 1] == '.' and s[1][i] == '.':
            print('No')
            exit()

# 下辺
for i in range(1, W - 1):
    if s[H - 1][i] == '#':
        if s[H - 1][i - 1] == '.' and s[H - 1][i + 1] == '.' and s[H - 2][i] == '.':
            print('No')
            exit()

# 左辺
for i in range(1, H - 1):
    if s[i][0] == '#':
        if s[i - 1][0] == '.' and s[i + 1][0] == '.' and s[i][1] == '.':
            print('No')
            exit()

# 右辺
for i in range(1, H - 1):
    if s[i][W - 1] == '#':
        if s[i - 1][W - 1] == '.' and s[i + 1][W - 1] == '.' and s[i][W - 2] == '.':
            print('No')
            exit()

# 中央
for h in range(1, H - 1):
    for w in range(1, W - 1):
        if s[h][w] == '#':
            if s[h - 1][w] == '.' and s[h + 1][w] == '.' and s[h][w - 1] == '.' and s[h][w + 1] == '.':
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
H, W = list(map(lambda n: int(n), input().split(" ")))
S = []
for i in range(H):
  if i == 0:
    S.append("X" * (W + 2))
  S.append("X" + input() + "X")
else:
  S.append("X" * (W + 2))

unpaintable = False
for i in range(1, H+1):
  for j in range(1, W+1):
    if S[i][j] == "#" and "#" not in [S[i-1][j], S[i+1][j], S[i][j-1], S[i][j+1]]:
      unpaintable = True
      break
      
print("No") if unpaintable else print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [[]for _ in range(h)]
for i in range(h):
    s[i] = list(input())

ans = [["." for _ in range(w)] for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            if i != 0:
                if s[i-1][j] == "#":
                    ans[i-1][j] = "#"
            if i != h-1:
                if s[i+1][j] == "#":
                    ans[i+1][j] = "#"
            if j != 0:
                if s[i][j-1] == "#":
                    ans[i][j-1] = "#"
            if j != w-1:
                if s[i][j+1] == "#":
                    ans[i][j+1] = "#"
print("Yes" if s == ans else "No")

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
L=[[0 for i in range(W)] for j in range(H)]
for i in range(H):
  L1=list(input())
  L[i]=L1
L = [["."] + i + ["."] for i in L]
L = [["." for i in range(W+2)]] + L + [["." for i in range(W+2)]]
for i in range(H+2):
  for j in range(W+2):
    if L[i][j]=="#":
      if L[i][j+1]==L[i][j-1]==L[i+1][j]==L[i-1][j]==".":
        print("No")
        exit()
print("Yes")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [input() for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            if i-1 >= 0 and i+1 < h and j-1 >= 0 and j+1 < w:
                if s[i-1][j] != "#" and s[i+1][j] != "#" and s[i][j-1] != "#" and s[i][j+1] != "#":
                    print("No")
                    exit()
print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]

ans = True

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            t = False
            if i != 0:
                t = max(t, S[i - 1][j] == "#")
            if i != H - 1:
                t = max(t, S[i + 1][j] == "#")
            if j != 0:
                t = max(t, S[i][j - 1] == "#")
            if j != W - 1:
                t = max(t, S[i][j + 1] == "#")

            if not t:
                ans = False
                break

if ans:
    print("Yes")

else:
    print("No")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
f = True
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            dx = [0]
            dy = [0]
            if i != 0:
                dx.append(-1)
            if i != h - 1:
                dx.append(1)
            if j != 0:
                dy.append(-1)
            if j != w - 1:
                dy.append(1)
            ff = False
            for x in dx:
                for y in dy:
                    if abs(x) == abs(y):
                        continue
                    ff |= (s[i + x][j + y] == "#")
            f &= ff

print("Yes" if f else "No")

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
S = [list(input()) for _ in range(H)]

ans = "Yes"
for h in range(H):
  for w in range(W):
    if S[h][w] == "#":
      flag = 0
      
      if h == 0:
        h1 = [1]
      elif h == H-1:
        h1 = [H-2]
      else:
        h1 = [h-1,h+1]
      
      if w == 0:
        w1 = [1]
      elif w == W-1:
        w1 = [W-2]
      else:
        w1 = [w-1,w+1]
        
      for h0 in h1:
        if S[h0][w] =="#":
          flag = 1
          
      for w0 in w1:
        if S[h][w0] == "#":
          flag = 1
       
      if flag == 0:
        ans = "No"
        break
print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [["."] + list(input()) + ["."] for _ in range(H)]
S = [["."]*(W+2)] + S + [["."]*(W+2)]

ans = "Yes"
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j] == "#":
            UDLR = [S[i-1][j], S[i+1][j], S[i][j-1], S[i][j+1]]
            if "#" in UDLR:
                pass
            else:
                ans = "No"
                break

print(ans)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W = map(int, readline().split())
    S = [readline().strip() for _ in range(H)]

    def check(i, j):
        if (
            i - 1 >= 0
            and S[i - 1][j] == '#'
            or i + 1 < H
            and S[i + 1][j] == '#'
            or j - 1 >= 0
            and S[i][j - 1] == '#'
            or j + 1 < W
            and S[i][j + 1] == '#'
        ):
            return True
        else:
            return False

    for i in range(H):
        for j in range(W):
            if S[i][j] == '#':
                ok = check(i, j)
                if not ok:
                    print('No')
                    return

    print('Yes')
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#coding: utf-8
#from collections import defaultdict, deque

H, W = (int(x) for x in input().split())

paint = []
for i in range(H):
    paint.append(list(input()))

def has_adjacent(i, j):
    for next_i, next_j in ((i-1, j), (i+1, j), (i, j-1), (i, j+1)):
        if next_i < 0 or next_i >= H or next_j < 0 or next_j >= W:
            continue
        if paint[next_i][next_j] == '#':
            return True
    return False

for i in range(H):
    for j in range(W):
        if paint[i][j] == '#':
            if not has_adjacent(i, j):
                print("No")
                exit(0)

print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = ["."*(W+2)] + ["." + input() + "." for _ in range(H)] + ["."*(W+2)] #塗りたし

for y in range(1, H+1):
    for x in range(1, W+1):
        if S[y][x] == '#' and (S[y-1][x] + S[y+1][x] + S[y][x-1] + S[y][x+1]).count('#') == 0:
            print("No")
            exit()
print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
 
f = False
for h in range(H):
  pre = '.'
  now = S[h][0]
  nxt = S[h][1]
  abv = '.' if h == 0 else S[h-1][0]
  blw = '.' if h == H-1 else S[h+1][0]
  for w in range(W):
    if now == '.':
      f = False
    elif now in [pre, nxt, abv, blw]:
      f = False
    else:
      f = True
    
    pre = now
    now = nxt
    nxt = '.' if w == W-1 else S[h][w+1]
    abv = '.' if h == 0 else S[h-1][w]
    blw = '.' if h == H-1 else S[h+1][w]
    
    if f:
      print('No')
      exit()
print('Yes')