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:
h,w = map(int,input().split())
l = [['']*(w+2)]
for i in range(h):
    l.append(['']+list(input())+[''])
l.append(['']*(w+2))
for i in range(1,h+1):
    for j in range(1,w+1):
        if l[i][j] == '#':
            flag = False
            for k in [(-1,0),(1,0),(0,-1),(0,1)]:
                if l[i + k[0]][j + k[1]] == '#':
                    flag = True
            if not flag:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [""]*H
for i in range(H):
    S[i] = list(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 j < W-1:
                if S[i][j+1] == "#":
                    continue
            if i < H-1 :
                if S[i+1][j] == "#":
                    continue
                
            print("No")
            exit()

print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
cambus = [list(input()) for _ in range(h)]
for i in range(h):
    for j in range(w):
        if cambus[i][j] == '#':
            if i-1 >= 0 and cambus[i-1][j] == '#':
                continue
            elif j-1 >= 0 and cambus[i][j-1] == '#':
                continue
            elif i+1 <= h-1 and cambus[i+1][j] == '#':
                continue
            elif j+1 <= w-1 and cambus[i][j+1] == '#':
                continue
            else:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

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

            if i - 1 >= 0:
                append(s[i - 1][j])
            if i + 1 < H:
                append(s[i + 1][j])
            if j - 1 >= 0:
                append(s[i][j - 1])
            if j + 1 < W:
                append(s[i][j + 1])

            if set(check) == {"."}:
                print("No")
                exit(0)
print("Yes")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
grid = []
grid.append(["."]*(w+2))
for i in range(h):
    grid.append(["."]+list(input())+["."])
grid.append(["."]*(w+2))
dx = [0,1,0,-1]
dy = [1,0,-1,0]
for i in range(1,h+1):
    for j in range(1,w+1):
        if grid[i][j] == "#":
            ok = False
            for k in range(4):
                if grid[i+dy[k]][j+dx[k]] == "#":
                    ok = True
            if not ok:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

def f():
    a = [(-1, 0), (0, -1), (0, 1), (1, 0)]
    for h in range(H):
        for w in range(W):

            if s[h][w] == "#":
                f = True
                for x, y in a:
                    h2 = h + x
                    w2 = w + y
                    if 0 <= h2 < H and 0 <= w2 < W:
                        if s[h2][w2] == "#":
                            f = False
                if f:
                    print("No")
                    return
    print("Yes")
    return

f()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
G = []
for i in range(H):
    G.append(list(input()))
fd = [[-1, 0], [1, 0], [0, 1], [0, -1]]
for i in range(H-1):
    for j in range(W-1):
        if G[i][j] == '#':
            boo = False
            for x, y in fd:
                if i+y < 0 or i+y >= H or j+x < 0 or j+y >= W:
                    continue
                if G[i+y][j+x] == '#':
                    boo = True
            if not boo:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
G = []
for i in range(H):
    G.append(list(input()))
fd = [[-1, 0], [1, 0], [0, 1], [0, -1]]
for i in range(H-1):
    for j in range(W-1):
        if G[i][j] == '#':
            boo = False
            for x, y in fd:
                if i+y < 0 or i+y >= H:
                    continue
                if j+x < 0 or j+y >= W:
                    continue
                if G[i+y][j+x] == '#':
                    boo = True
            if not boo:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
masu = []
for _ in range(H):
  tmp = list(input())
  masu.append(tmp)
  
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]

for i in range(H):
  for j in range(W):
    if masu[i][j] == ".":
      continue
    is_ok = False
    for dy, dx in directions:
      new_y = i + dy
      new_x = j + dx
      if new_y >= H or new_x >= W:
        continue
      if masu[new_y][new_x] == "#":
        is_ok = True
        break
    if is_ok == False:
      print('No')
      quit()
print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[]
s.append("."*(w+2))
for _ in range(h):
    s.append("."+input()+".")
s.append("."*(w+2))


flag=True



for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j]=="#":
            if s[i-1][j]=="." and s[i][j-1]=="." and s[i][j+1]=="."  and s[i+1][j]=="." :
                flag=False

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

Submission status: Accepted

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

#全部の黒のとこ見て上下左右に黒があればおk！
x = [0,0,1,-1]
y = [1,-1,0,0]

ans = "Yes"
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            flag = False
        else:
            continue
        for k in range(4):
                if 0<=i+x[k]<h and 0<=j+y[k]<w: 
                    if s[i+x[k]][j+y[k]] == "#":
                        flag = True
                    else:
                        continue
        if flag:
            continue
        else:
            print("No")
            exit()


print(ans)

Submission status: Accepted

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

for x in range(1,w+1):
  for y in range(1, h+1):
    if mat[y][x] == "#" and mat[y-1][x] == "." and mat[y+1][x] == "." and mat[y][x-1] == "." and mat[y][x+1] == ".":
      print("No")
      exit()
print("Yes")

Submission status: Accepted

Solution code:
def main():
    H,W=map(int,input().split())
    m=[input() for _ in range(H)]
    is_ok = True
    for h in range(H):
        for w in range(W):
            if m[h][w] == "#":
                if -1 < h-1 and m[h-1][w] == "#":
                    pass
                elif -1 < w-1 and m[h][w-1] == "#":
                    pass
                elif h+1 < H and m[h+1][w] == "#":
                    pass
                elif w+1 < W and m[h][w+1] == "#":
                    pass
                else:
                    print("No")
                    return
    print("Yes")
        
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
S = [list(input()) for i in range(h)]
for i in range(h):
  S[i] = ["."] + S[i] + ["."]
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] == ".":
      pass
    else:
      if [S[i-1][j],S[i+1][j],S[i][j-1],S[i][j+1]] == ["."]*4:
        print("No")
#        print(i,j)
        exit()
      
print("Yes")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [0]*(h+2)
s[0] = ["."]*(w+2)
s[-1] = ["."]*(w+2)
for i in range(1,h+1):
    s[i] = ["."] + list(input()) + ["."]
ans = 1
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == "#":
            if s[i-1][j] == "." and s[i+1][j] == "." and s[i][j-1] == "." and s[i][j+1] == ".":
                ans = 0
                break
print(["No","Yes"][ans])

Submission status: Accepted

Solution code:
#! env python
# -*- coding: utf-8 -*-

import os
import sys

from itertools import repeat


# ac_py.main
# Date: 2020/06/14
# Filename: main 
# Author: acto_mini

def main():
    H, W = map(int, input().split())

    S = list()

    for i in range(H):
        S.append(input())

    for i in range(H):
        for j in range(W):
            if S[i][j] == "#":
                # 上
                if i > 0 and S[i - 1][j] == "#":
                    continue
                # 左
                elif j > 0 and S[i][j - 1] == "#":
                    continue
                # 右
                elif j < (W - 1) and S[i][j + 1] == "#":
                    continue
                # 下
                elif i < (H - 1) and S[i + 1][j] == "#":
                    continue
                else:
                    print("No")
                    exit()

    print("Yes")
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    for y in range(H):
        for x in range(W):
            if s[y][x] == '.':
                continue
            else:
                for dx,dy in [-1,0],[0,-1],[1,0],[0,1]:
                    nx = x + dx
                    ny = y + dy
                    if (0 <= nx < W) and (0 <= ny < H) and s[ny][nx] != '.':
                        break
                    elif (0 > nx) or (W <= nx) or (0 > ny) or (H <= ny):
                        continue
                    elif dx == 0 and dy == 1 and s[ny][nx] == '.':
                        return 'No'

    return 'Yes'

print(main())

Submission status: Accepted

Solution code:
def check_adjacents(S, i, j):
    return i > 0 and S[i-1][j] == '#' or \
           i < H - 1 and S[i+1][j] == '#' or \
           j > 0 and S[i][j-1] == '#' or \
           j < W - 1 and S[i][j+1] == '#'

def solution(H, W, S):
    for i in range(H):
        for j in range(W):
            if S[i][j] == '#':
                adjacents_sharp = check_adjacents(S, i, j)
                if not adjacents_sharp:
                    return 'No'
    return 'Yes'

H, W = map(int, input().split())
S = [input() for _ in range(H)]
print(solution(H, W, S))

Submission status: Accepted

Solution code:
h,  w =  map(int, input().split())
a = []
a.append("."*(w+2))
for _ in  range(h):
    a.append("."+input()+".")
a.append("."*(w+2))

ans  = "Yes"
for i in range(h+2):
    for j in  range(w+2):
        if a[i][j] == "#":
            if not(a[i-1][j] == "#" or a[i+1][j] == "#" or a[i][j-1] == "#" or a[i][j+1] == "#"):
                ans = "No"
                break
print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())

grid = [['.' for _ in range(W + 2)]]
for _ in range(H):
    g = ['.'] + [i for i in input()] + ['.']
    grid.append(g)

grid.append(['.' for _ in range(W + 2)])

f = 1
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if grid[h][w] == '.':
            continue
        a = grid[h - 1][w] == '#'
        b = grid[h + 1][w] == '#'
        c = grid[h][w - 1] == '#'
        d = grid[h][w + 1] == '#'
        if not any([a, b, c, d]):
            f = 0
            break
    
    if not f:
        break

print(['No', 'Yes'][f])

Submission status: Accepted

Solution code:
__DEUBG__ = 0

import math
import sys

H, W = map(int, input().split())

board = [
  list(input())
  for _ in range(H)]

def calc(board, x, y):
    if board[x][y] == '#':
        for i,j in [
          (x, y-1),
          (x-1, y),
          (x+1, y),
          (x, y+1)
        ]:
            if 0 <= i <= H-1 and 0 <= j <= W-1:
                if board[i][j] == '#':
                    return
        print('No')
        exit()
    else:
        return

for i in range(H):
    for j in range(W):
        if __DEUBG__:
            print(i, j)
        calc(board, i, j)

print('Yes')

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
S = np.array([list(sr()) for _ in range(H)])
black = S == '#'
is_ok = np.zeros_like(S, dtype=np.bool)
is_ok[1:, :] |= black[:-1, :]
is_ok[:-1, :] |= black[1:, :]
is_ok[:, 1:] |= black[:, :-1]
is_ok[:, :-1] |= black[:, 1:]
impossible = (black & (~is_ok)).any()
print('No' if impossible else 'Yes')

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
S = [sr() for _ in range(H)]
bl = True
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            continue
        for dy, dx in zip([0, 1, 0, -1], [1, 0, -1, 0]):
            ny = i + dy
            nx = j + dx
            if 0 <= ny < H and 0 <= nx < W and \
                    S[ny][nx] == '#':
                break
        else:
            bl = False

print('Yes' if bl else 'No')

Submission status: Accepted

Solution code:
import numpy as np
h,w = list(map(int, input().split())) 
canvas = np.array([[np.nan]*w]*h).astype(int)

for i in range(h):
    s = input()
    for j in range(w):
        if( s[j] == '.'):
            canvas[i, j] = 0
        elif (s[j]=='#'):
            canvas[i, j] = 1

ret = True
for i in range(h):
    for j in range(w):
        if( canvas[i, j] == 1):

            check = False
            ch = i-1
            cw = j
            if( (0 <= ch & ch < h) & (0 <= cw & cw < w)):  
                if( canvas[ch,cw] == 1):
                    check = True
            ch = i
            cw = j-1
            if( (0 <= ch & ch < h) & (0 <= cw & cw < w)):  
                if( canvas[ch,cw] == 1):
                    check = True
            ch = i
            cw = j+1
            if( (0 <= ch & ch < h) & (0 <= cw & cw < w)):  
                if( canvas[ch,cw] == 1):
                    check = True
            ch = i+1
            cw = j
            if( (0 <= ch & ch < h) & (0 <= cw & cw < w)):  
                if( canvas[ch,cw] == 1):
                    check = True  
            if check == False:
                ret = False
                break
    if ret == False:
        break
if ret:
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

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

def check(i,j):
    for x,y in check_list:
        try: 
            if s_a[i+x][j+y] == '#':
                return True
        except:
            continue
    return False

ans = 'Yes'
for i in range(H):
    for j in range(W):
        if s_a[i][j] == '#':
            if check(i,j):
                continue
            else:
                ans = 'No'
                break
print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
grid = [input() for _ in range(h)]
dxy = [[1, 0], [-1, 0], [0, 1], [0, -1]]

for i in range(1, h - 1):
    for j in range(1, w - 1):
        if grid[i][j] == '#':
            for a, b in dxy:
                if grid[i + a][j + b] == '#':
                    break
            else:
                print("No")
                exit()

print("Yes")

Submission status: Accepted

Solution code:
h, w = [int(i) for i in input().split()]
paint = [list(input()) for j in range(h)]
ans = "Yes"

for i in range(h):
    for j in range(w):

        if paint[i][j] == "#":
            left = paint[i][j-1] == "#" if j > 0 else False
            right = paint[i][j+1] == "#" if j < w-1 else False
            top = paint[i-1][j] == "#" if i > 0 else False
            bottom = paint[i+1][j] == "#" if i < h-1 else False

            if not any([left, right, top, bottom]):
                ans = "No"
                break
    if ans == "No":
        break

print(ans)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())

s = [list('.'*(w+2))]
for _ in range(h):
    s.append(list('.'+input()+'.'))
s.append(list('.'*(w+2)))

for i in range(h):
    for j in range(w):
        if s[i+1][j+1] == '#':
            if s[i+2][j+1] == '.' and s[i][j+1] == '.' and s[i+1][j+2] == '.' and s[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:
                if j - 1 < 0:
                    if s[i][j + 1] == '#' or s[i + 1][j] == '#':
                        continue
                    else:
                        print('No')
                        exit()
                elif j + 1 >= W:
                    if s[i][j - 1] == '#' or s[i + 1][j] == '#':
                        continue
                    else:
                        print('No')
                        exit()
                else:
                    if s[i][j - 1] == '#' or s[i][j + 1] == '#' or s[i + 1][
                        j] == '#':
                        continue
                    else:
                        print('No')
                        exit()
            elif i + 1 >= H:
                if j - 1 < 0:
                    if s[i][j + 1] == '#' or s[i - 1][j] == '#':
                        continue
                    else:
                        print('No')
                        exit()
                elif j + 1 >= W:
                    if s[i][j - 1] == '#' or s[i - 1][j] == '#':
                        continue
                    else:
                        print('No')
                        exit()
                else:
                    if s[i][j - 1] == '#' or s[i][j + 1] == '#' or s[i - 1][
                        j] == '#':
                        continue
                    else:
                        print('No')
                        exit()
            else:
                if j - 1 < 0:
                    if s[i][j + 1] == '#' or s[i + 1][j] == '#' or s[i - 1][
                        j] == '#':
                        continue
                    else:
                        print('No')
                        exit()
                elif j + 1 >= W:
                    if s[i][j - 1] == '#' or s[i + 1][j] == '#' or s[i - 1][
                        j] == '#':
                        continue
                    else:
                        print('No')
                        exit()
                else:
                    if s[i][j - 1] == '#' or s[i][j + 1] == '#' or s[i + 1][
                        j] == '#' or s[i - 1][j] == '#':
                        continue
                    else:
                        print('No')
                        exit()
print('Yes')

Submission status: Accepted

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

print('Yes')

Submission status: Accepted

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

for h in range(H):
	for w in range(W):
		if S[h][w]=="#":
			if h>0:
				l.append(S[h-1][w])
			if h<H-1:
				l.append(S[h+1][w])
			if w>0:
				l.append(S[h][w-1])
			if w<W-1:
				l.append(S[h][w+1])
			if "#" not in l:
				flag=1
				break
			l=[]

print("Yes" if flag==0 else "No")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
canvas = [[i for i in input()]]
for i in range(H - 1):
    tmp = [i for i in input()]
    canvas.append(tmp)
direction = [[-1, 0], [0, -1], [0, 1], [1, 0]]
for i in range(H):
    for j in range(W):
        if canvas[i][j] == "#":
            canDraw = False
            for k in direction:
                if (i - k[0] >= 0 and i - k[0] < H) and (j - k[1] >= 0 and j - k[1] < W):
                    if canvas[i - k[0]][j - k[1]] == "#":
                        canDraw = True
                        break
            if not canDraw:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

flag=True
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j]=='#' and s[i-1][j]=='.' and s[i][j-1]=='.' and s[i+1][j]=='.' and s[i][j+1]=='.':
            flag=False
print('Yes' if flag else 'No')

Submission status: Accepted

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

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+1][j]==s[i][j-1]==s[i][j+1]==".":
                flg = False

print('Yes' if flg else 'No')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())

s = [list(input()) for _ in range(H)]

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

for i in range(H):
    for j in range(W):
        can = False
        if s[i][j] == "#":
            for k in range(4):
                x = i + dx[k]
                y = j + dy[k]
                if 0 <= x <= H - 1 and 0 <= y <= W - 1:
                    if s[x][y] == "#":
                        can = True
            if can is False:
                print("No")
                exit()

print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
hw = [list(input()) for _ in range(h)]
num = 0
for i in range(h):
    num+=hw[i].count('#')

def check(i, j):
    if not (0<=i<h and 0<=j<w):
        return False
    if hw[i][j] == '#':
        return True
    return False

checked = set()
for i in range(h):
    for j in range(w):
        if hw[i][j] == '#':
            if (i,j) in checked:
                continue

            exist = False

            for di,dj in [(1,0),(-1,0),(0,1),(0,-1)]:
                ci = i + di
                cj = j + dj
                if check(ci, cj):
                    checked.add((ci,cj))
                    exist = True
            if exist:
                checked.add((i,j))
if num == len(checked):
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

for i in range(h + 2):
    for j in range(w + 2):
        if s[i][j] == '#' and 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())
a = [input() for _ in range(h)]
flag = True
def check(x, y):
    candidates = []
    if x > 0:
        candidates.append(a[x - 1][y])
    if x < h - 1:
        candidates.append(a[x + 1][y])
    if y > 0:
        candidates.append(a[x][y - 1])
    if y < w - 1:
        candidates.append(a[x][y + 1])
    if '#' in candidates:
        return True
    else:
        return False

for i in range(w):
    for j in range(h):
        if a[j][i] == '#':
            if check(j, i):
                pass
            else:
                flag = False
                break
print('Yes') if flag else print('No')

Submission status: Accepted

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

if h == 1 or w == 1:
    print("No")
    exit()

for i in range(h):
    for j in range(w):
        x = 0 #縦OK
        y = 0 #横OK
        if li[i][j] == "#":
            if i == 0:
                if li[i+1][j] == "#":
                    x = 1
            elif i == h-1:
                if li[i-1][j] == "#":
                    x = 1
            else:
                if li[i+1][j] == "#" or li[i-1][j] == "#":
                    x = 1
            if j == 0:
                if li[i][j+1] == "#":
                    y = 1
            elif j == w-1:
                if li[i][j-1] == "#":
                    y = 1
            else:
                if li[i][j+1] == "#" or li[i][j-1] == "#":
                    y = 1 

            if x == 0 and y == 0:
                print("No")
                exit()

print("Yes")

Submission status: Accepted

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

for i in range(h+2) :
    s[i].insert(0,0)
    s[i].append(0)

around = [[0,1],[0,-1],[1,0],[-1,0]]

for i in range(1,h+1) :
    for j in range(1,w+1) :
        if s[i][j] == '#' :
            ans = False
            for k in range(4) :
                if s[i-around[k][0]][j-around[k][1]] == '#' :
                    ans = True
            if not ans  :
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
g=[list(input()) for _ in range(h)]
for x in range(h):
    for y in range(w):
        if g[x][y]=="#":
            k=0
            for i,j in [(1,0),(-1,0),(0,1),(0,-1)]:
                nx=x+i
                ny=y+j
                if nx<0 or nx>=h or ny<0 or ny>=w:
                   continue
                if g[nx][ny]==".":
                    k+=1
            if k==4:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
h, w = [int(i) for i in input().split()]
s = [input() for j in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            continue
        f = False
        if j > 0:
            if s[i][j-1] == "#":
                f = True
        if j < w-1:
            if s[i][j+1] == "#":
                f = True
        if i > 0:
            if s[i-1][j] == "#":
                f = True
        if i < h-1:
            if s[i+1][j] == "#":
                f = True
        if not f:
            print("No")
            exit()
print("Yes")

Submission status: Accepted

Solution code:
import sys

H, W = map(int, input().split())
MAP = [list(input()) for i in range(H)]
gawa = ['.' for i in range(W)]
MAP.append(gawa)
MAP.insert(0, gawa)

for i in range(H+2):
    MAP[i].append('.')
    MAP[i].insert(0, '.')

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

for i in range(1, H+1):
    for j in range(1, W+1):
        if MAP[i][j] == '#':
            FRAG = 'No'
            for k in range(4):
                if MAP[i + dx[k]][j + dy[k]] == '#':
                    FRAG = True
            if FRAG != True:
                print(FRAG)
                sys.exit()

print('Yes')

Submission status: Accepted

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

Submission status: Accepted

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

print('Yes')

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
grid = []
for i in range(n):
    grid.append(list(input()))
dr = [0, 0, -1, 1]
dc = [1, -1, 0, 0]
base = True
for i in range(n):
    for j in range(m):
        if grid[i][j] == '#':
            flag = False
            for k in range(4):
                newx = i + dr[k]
                newy = j + dc[k]
                if newx < 0 or newx >= n or newy < 0 or newy >= m or grid[newx][newy] == '.':
                    continue
                flag = True
            if not flag:
                print('No')
                base = False
                break
    if not base:
        break
if base:
    print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())

s = ['.'*(W+2)] + ['.'+input()+'.' for _ in range(H)] + ['.'*(W+2)]
for hi in range(1, H+1):
  for wi in range(1, W+1):
    if s[hi][wi] == '.':
      continue
    if s[hi-1][wi]=='.' and s[hi+1][wi]=='.':
      if s[hi][wi-1]=='.' and s[hi][wi+1]=='.':
        print('No')
        exit()
        
print('Yes')

Submission status: Accepted

Solution code:
from itertools import product

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


def has_neighbor(_i: int, _j: int) -> bool:
    patterns = [
        (-1, 0), (1, 0), (0, -1), (0, 1),
    ]
    cnt = 0
    for pattern in patterns:
        y, x = _i + pattern[0], _j + pattern[1]
        if y in range(H) and x in range(W):
            c = S[y][x]
            if c == "#":
                cnt += 1
        else:
            pass
    if cnt:
        return True
    else:
        return False


for i, j in product(range(H), range(W)):
    if S[i][j] == "#" and not has_neighbor(i, j):
        print("No")
        break
else:
    print("Yes")