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:
def main():
    h, w = list(map(int, input().split()))
    canvas = []
    for i in range(h):
        canvas.append(list(input()))
    can_draw = True
    for i in range(h):
        for j in range(w):
            if canvas[i][j] == '.':
                continue
            s = []
            # 上下
            if i != 0:
                s.append(canvas[i - 1][j])
            if i != h - 1:
                s.append(canvas[i + 1][j])
            # 左右
            if j != 0:
                s.append(canvas[i][j - 1])
            if j != w - 1:
                s.append(canvas[i][j + 1])
            if s.count('#') == 0:
                can_draw = False
    print('Yes') if can_draw else print('No')



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = ["." + input() + "." for _ in range(H)]
s = ["." * (W + 2)] + s + ["." * (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] == "#" or s[i + 1][j] == "#" or s[i][j - 1] == "#" or s[i][j + 1] == "#":
                pass
            else:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

ans = 'Yes'
for i in range(1,h+1):
    for j in range(1,w+1):
        if s_l[i][j] == '#':
            if '#' in (s_l[i-1][j], s_l[i+1][j], s_l[i][j-1], s_l[i][j+1]):
                continue
            else:
                ans = 'No'
                break
    if ans == 'No':
        break

print(ans)

Submission status: Accepted

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

def check(x,y):
    global flag
    for dx,dy in ((-1,0),(1,0),(0,-1),(0,1)):
        sx = x + dx
        sy = y + dy
        if sx < 0 or sx >= W or sy < 0 or sy >= H:
            continue
        if s[sy][sx] == '#':
            break
    else:
        flag = False
    return flag

for y in range(H):
    for x in range(W):
        if s[y][x] == '.':
            continue
        flag = check(x,y)
print('Yes' if flag else 'No')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    H, W = map(int, input().split())
    canvas = [ list(input()) for i in range(H)]
    possible = True

    for i in range(H):
        for j in range(W):
            if canvas[i][j] == "#":
                if j - 1 >= 0 and canvas[i][j-1] == "#":
                    pass
                elif j + 1 < W and canvas[i][j+1] == "#":
                    pass
                elif i - 1 >= 0 and canvas[i-1][j] == "#":
                    pass
                elif i + 1 < H and canvas[i+1][j] == "#":
                    pass
                else:
                    possible = False
                    
    if possible:
        print("Yes")
    else:
        print("No")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
S = ["."*(w+2)]

for i in range(h):
    S.append("."+input()+".")
    
S.append("."*(w+2))

ans = "Yes"

for i in range(1,h+1):
    for j in range(1,w+1):
        if S[i][j] == "#":
            if S[i-1][j] != "#":
                if S[i][j-1]!="#" and S[i][j+1]!="#":
                    if S[i+1][j] != "#":
                        ans = "No"
                        break
                    
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    H, W = [int(item) for item in input().split()]

    grid = [[item for item in input()] for _ in range(H)]

    # print(grid)

    def chk(i,j):
        next_node_list = [
            [max(0, i-1), j],
            [min(H-1, i+1), j],
            [i, max(0, j-1)],
            [i, min(W-1, j+1)]
        ]


        if grid[i][j] == '#':
            for next_node in next_node_list:
                if next_node == [i, j]:
                    pass
                else:
                    if grid[next_node[0]][next_node[1]] == '#':
                        return True
            else:
                return False
        else:
            return True


    def loop_chk(H, W):
        for i in range(H):
            for j in range(W):
                # print(i,j, chk(i,j))
                if chk(i,j):
                    pass
                else:
                    return False
        else:
            return True

    if loop_chk(H, W):
        print('Yes')
    else:
        print('No')
    

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a=[[str(i) for i in input()] for j in range(h)]
ef=True
for y in range(h):
    for x in range(w):
        f=False
        if a[y][x]=="#":
            if y>0:
                if a[y-1][x]=="#":f=True
            if x>0:
                if a[y][x-1]=="#":f=True
            if y+1!=h:
                if a[y+1][x]=="#":f=True
            if x+1!=w:
                if a[y][x+1]=="#":f=True
            if not f:
                ef=False
                break
print("Yes" if ef else "No")

Submission status: Accepted

Solution code:
def func(x,y):
    global s,h,w,seen
    d = [1,-1]
    flag = False
    for i in d:
        if x+i < h and x+i >= 0:
            if s[x+i][y] == "#":
                seen[(x+i)*w+y] = 1
                seen[x*w+y] = 1
                flag = True
        if y+i < w and y+i >= 0:
            if s[x][y+i] == "#":
                seen[x*w+y+i] = 1
                seen[x*w+y] = 1
                flag = True
    return flag


h,w = map(int,input().split())
s = [input() for _ in range(h)]
seen = [0 for _ in range(h*w)]
ans = False
for i in range(h):
    for j in range(w):
        if s[i][j] == "#" and seen[j + i*w] == 0:
            if not func(i,j):
                ans = True
if ans:
    print("No")
else:
    print("Yes")

Submission status: Accepted

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



for j in range(len(s) - 1):
    for i in range(len(s[j]) - 1):
        if s[j][i] == "#" and s[j][i] != s[j][i+1] and (s[j][i] != s[j][i-1] or i == 0):
            if s[j][i] != s[j+1][i] and (s[j][i] != s[j-1][i] or j == 0):
                print("No")
                exit()

print("Yes")

Submission status: Accepted

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

A = []

B = ['.'] * (w + 2)
A.append(B)
for i in range(h):
    C = list(input())
    C = ['.'] + C + ['.']
    A.append(C)
A.append(B)

ans = 1
for i in range(1, h+1):
    for j in range(1, w+1):
        if A[i][j] == '#':
            D = [A[i - 1][j], A[i][j - 1], A[i][j + 1], A[i + 1][j]]
            if '#' in D:
                continue
            else:
                ans = 0
                break

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

Submission status: Accepted

Solution code:
import sys

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

def repaint(x, y):
    if S[x][y] == ".":
        T[x][y] = 1
    if S[x][y] == "#":
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < H and 0<= ny < W and S[nx][ny] == "#":
                T[x][y] = 1



H, W = map(int, sys.stdin.readline().split())
S = [list(sys.stdin.readline().rstrip()) for i in range(H)]
T = [[0] * W for i in range(H)]

for i in range(H):
    for j in range(W):
        repaint(i, j)
        if T[i][j] == 0:
            print("No")
            exit()
print("Yes")

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
endmap=[list(s for s in input()) for _ in range(H)]
exlarge=[[0]*(W+2) for _ in range(H+2)]
ans='Yes'
for i in range(H):
  for j in range(W):
    if endmap[i][j]=='#':
      exlarge[i+1][j+1]=1
 
for k in range(H+2):
  for l in range(W+2):
    if exlarge[k][l]==1 and exlarge[k][l-1]== exlarge[k][l+1]==exlarge[k-1][l]==exlarge[k+1][l]==0:
          ans='No'
 
print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
G = [list('.'*(W+2)) for _ in range(H+2)]
for i in range(H):
  G[i+1][1:W+1] = input()
ans = 'Yes'
for i in range(1,H):
  for j in range(1,W):
    if G[i][j]=='#':
      if G[i-1][j]=='.' and G[i+1][j]=='.' and G[i][j-1]=='.' and G[i][j+1]=='.':
        ans = 'No'
        break
print(ans)

Submission status: Accepted

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

        # デフォルトが白色のキャンバスにおいて、square1001君は指定の場所を 黒色にしたい。
        # しかし、 "." の場合はキャンバスを白色 に染めるという操作のため、スキップして問題ない
        if S[i][j] == ".": continue

        # 黒色に染めたい場所がある（S[i][j] == "#"）時は、
        # 上下左右のマスを調べて、１つでも "#" があれば良い（上下左右に隣接する 2つのマスを選び黒く塗らなくてはならないから）

        # 左、上、右、下
        # ex. (1,1)
        # (0,1), (1,2), (2,1), (1,0)
        dx = [-1, 0, 1, 0]
        dy = [0, -1, 0, 1]
        is_exist = False

        # H = 1, W = 10
        # . . . . . . . # # #
        # => "Yes"
        # (x,y)
        # (0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0), (9, 0), (10, 0)
        # 上と下は定義されていないので範囲外
        # (0, 0)は左が範囲外、(10,0)は右が範囲外
        # [ ['...'], ['###'] ]
        # i = 0, j = 0 => '...' S[i][j][0], S[i][j][1]


        for dir in range(4):
            x = i + dx[dir] # x座標
            y = j + dy[dir] # y座標

            # (0, 1)や(1,0)だと (-1,1)や(1,-1) で範囲外を見てしまうので、そこはスキップさせる
            if x < 0 or y < 0 or H <= x or W <= y: continue
            if S[x][y] == "#": is_exist = True
        
        # ある場所 (x, y) の上下左右に黒色に染めたいマスがなかった場合、条件を満たさないため No を出す
        if is_exist == False:
            print("No")
            exit(0) # プログラムを強制終了
print("Yes")

        # if H == 1 or W == 1:
        #     if i == 0 and H == 1 and S[i][j] != S[i][j+1]:
        #         count += 1
        #     elif j == 0 and W == 1 and S[i][j] != S[i+1][j]:
        #         count += 1
        # elif H >= j-2 and W >= i-2:
        #     if S[i][j] != S[i][j+1] or S[i][j] != S[i+1][j]:
        #         count += 1

# if count == 0:
#     print("Yes")
# else:
#     print("No")

Submission status: Accepted

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

s = [["."] * (W+2) for _ in range(H+1)]
for i in range(H):
  s[1+i][1:1+W] = input()

ans = "Yes"
for i in range(1, H):
  for j in range(1, W):
    if s[i][j] != "#":
      continue
    if s[i-1][j] == "." and s[i+1][j] == "." and s[i][j-1] == "." and s[i][j+1] == ".":
      ans = "No"
  
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
 
def main():
  
  row, col = map(int, input().split())
  ls2 = [['.']*(col+2)]
  for i in range(row):
    ls2.append(['.'] + list(input().rstrip()) + ['.'])
  ls2.append(['.']*(col+2))
  nda = np.array(ls2)
  
  rslt = 'Yes'
  for i in range(row):
    for j in range(col):
      if nda[i,j] == '#':
        if nda[i-1,j] != '#':
          if nda[i+1,j] != '#':
            if nda[i,j-1] != '#':
              if nda[i,j+1] != '#':
                rslt = 'No'
  print(rslt)
  
if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    H,W=map(int,input().split())
    s=[['.']*(W+2) for _ in range(H+2)]
    for i in range(1,H+1):
        s[i][1:W+1]=list(input())
    dy=[1,0,-1,0]
    dx=[0,1,0,-1]
    for i in range(1,H+1):
        for j in range(1,W+1):
            if s[i][j]=='#':
                for k in range(4):
                    if s[i+dy[k]][j+dx[k]]=='#':
                        break
                else:
                    print('No')
                    exit()
    print('Yes')

if __name__ == '__main__':
    main()

Submission status: Accepted

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

e=[]
for i in range(h):
    a=list(input())
    e.append(a)

def check(i,j,e):
    global h
    global w
    top=[i-1,j]
    bottom=[i+1,j]
    right=[i,j+1]
    left=[i,j-1]
    checkpoints=[top,bottom,left,right]
    if i==0:
        checkpoints.remove(top)
    if i==h-1:
        checkpoints.remove(bottom)
    if j==0:
        checkpoints.remove(left)
    if j==w-1:
        checkpoints.remove(right)
        
    if e[i][j]==".":
        return True
    else:
        kouho=[]
        for checkpoint in checkpoints:
            kouho.append(e[checkpoint[0]][checkpoint[1]])
        if "#" in kouho:
            return True
        else:
            return False
ans=0        
for i in range(h):
    for j in range(w):
        if not check(i,j,e):
            ans+=1
        
if ans==0:
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
# from collections import deque,Counter
# import re
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import networkx as nx
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb

# slist = "abcdefghijklmnopqrstuvwxyz"
# 盤面の受け取り
H,W = map(int,input().split())
board = [[] for _ in range(H)]
for i in range(H):
    board[i] = list(input())
moves = [(-1, 0), (0, -1), (0, 1),(1, 0)]

def check(h,w):
    for mh,mw in moves:
        x,y = h+mh,w+mw
        # print(x,y)
        if 0<=x<H and 0<=y<W and board[x][y]=="#":
            # print(board[x][y])
            return True
    return False
        
ans = "Yes"
for h in range(H):
    for w in range(W):
        if board[h][w]=="#":
            flg = check(h,w)
            if not flg:
                print("No")
                exit()

print(ans)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
ans = "Yes"
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] == ".":
            ans = "No"
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)


h, w = map(int, input().split())
maze = [[-1 for i in range(w)] for j in range(h)]

ans = 0
for i in range(h):
    maze[i] = input()

INF = 1000
ans = 0

for sy in range(h):
    for sx in range(w):
        if maze[sy][sx] == '.':
            continue

        flg = 0
        for i in range(0, 4):
            nx, ny = sx + [1, 0, -1, 0][i], sy + [0, 1, 0, -1][i]
            if 0 <= nx < w and 0 <= ny < h:
                if maze[ny][nx] == '#':
                    flg = 1
        if flg == 0:
            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)]
P=[(0,1),(0,-1),(1,0),(-1,0)]

for x,y in product(range(W),range(H)):
    if S[y][x]!="#":
        continue

    b_flg=False
    for p in P:
        c_y=y+p[0]
        c_x=x+p[1]
        if c_x <0 or c_x>=W or c_y<0 or c_y>=H: 
            continue
        if S[c_y][c_x]=="#":
            b_flg=True

    if b_flg==False:
        print("No")
        break
else:
    print("Yes")

Submission status: Accepted

Solution code:
# coding: utf-8

# https://atcoder.jp/contests/abc096/tasks/abc096_c
# 13:17-13:33


def main():
    H, W = map(int, input().split())
    s = [None] * (H+2)
    s[0] = [-1] * (W+2)
    s[-1] = [-1] * (W+2)
    for i in range(1, H+1):
        s[i] = [None] * (W+2)
        s[i][0] = -1
        s[i][-1] = -1
        for j, bw in enumerate(input()):
            s[i][j+1] = 1 if bw == "#" else -1

    if H == 1 and W == 1 and s[1][1] == 1:
        return "Yes"

    for i in range(1, H+1):
        for j in range(1, W+1):
            if s[i][j] == 1:
                if sum(s[i][j-1:j+2]) + s[i-1][j] + s[i+1][j] == -3:
                    return "No"
    
    return "Yes"


print(main())

Submission status: Accepted

Solution code:
import sys
H,W = map(int,input().split())
grid = []
for i in range(H):
    grid.append([el for el in input()])
move_x = [-1,1,0,0]
move_y = [0,0,1,-1]
for i in range(H):
    for j in range(W):
        if grid[i][j] == '#':
            counter = 0
            for k in range(4):
                i += move_x[k]
                j += move_y[k]
                if i < H and j < W and -1 < H and -1 < W:
                    if grid[i][j] == '#':
                        counter += 1
                i -= move_x[k]
                j -= move_y[k]
            if counter == 0:
                print('No')
                sys.exit(0)
print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
flag=True
count=0
for i in range(h):
  if flag==False:
    break
  for j in range(w):
    if flag==False:
      break
    flag2=False
    if s[i][j]=="#":
      if i>0 and s[i-1][j]!="#":
        flag=False
      elif i>0 and s[i-1][j]=="#":
        flag2=True
        
      if i<(h-1) and s[i+1][j]!="#":
        flag=False
      elif i<(h-1) and s[i+1][j]=="#":
        flag2=True
        
      if j>0 and s[i][j-1]!="#":
        flag=False
      elif j>0 and s[i][j-1]=="#":
        flag2=True
      
      if j<(w-1) and s[i][j+1]!="#":
        flag=False
      elif j<(w-1) and s[i][j+1]=="#":
        flag2=True
      if flag2:
        flag=True
    else:
      count+=1
        
if h==1 and w==1:
  if s[0][0]=="#":
    flag=False
    
if count==(h*w):
  flag=True
  
if flag:
  print("Yes")
else:
  print("No")

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].insert(0, ".")
    S[i].append(".")
S.insert(0, ["."]*(W+2))
S.append(["."]*(W+2))

filled = True
for i in range(1, H+2):
    for j in range(1, W+2):
        if (S[i][j] == "#") and (S[i][j-1] == S[i][j+1] == S[i-1][j] == S[i+1][j] == "."):
            filled = False

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

Submission status: Accepted

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

S = [list(input()) for i in range(H)]

for i in range(H):
    for j in range(W):
        b = 0
        if S[i][j] == "#":
            bottomi = i-1 if i > 0 else 0
            bottomj = j-1 if j > 0 else 0
            topi = i+1 if i < H-1 else H-1
            topj = j+1 if j < W-1 else W-1
            for k in range(bottomi,topi+1):
                for l in range(bottomj,topj+1):
                    if (k == i and l != j) or (k != i and l == j) :
                        if S[k][l] == "#":
                            b = 1
            else:
                if b == 0:
                    break
        else:
            b = -1
    if b == 0:
        print("No")
        break
else:
    print("Yes")

Submission status: Accepted

Solution code:
def myAnswer(H:int,W:int,S:list) -> int:
   for h in range(H):
      for w in range(W):
         target = S[h][w]
         if(target == "#"):
            if(h != 0 and S[h-1][w] == "#"):# 上を探索
               continue
            elif(w != 0 and S[h][w - 1] == "#"):# 右を探索
               continue
            elif(h != H-1 and S[h + 1][w] == "#"):# 下を探索
               continue
            elif(w != W -1 and S[h][w+1] == "#"):# 左を探索
               continue
            else:
               return "No"
   return "Yes"

def modelAnswer():
   return
def main():
   H,W = map(int,input().split())
   S = []
   for _ in range(H):
      S.append(list(input()))
   print(myAnswer(H,W,S[:]))
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s_list = [list(input()) for i in range(h)]
for i in range(h):
    for j in range(w):
        if s_list[i][j] == "#":
            flg = False
            if i != 0:
                if s_list[i - 1][j] == "#":
                    flg = True
            if i != h - 1:
                if s_list[i + 1][j] == "#":
                    flg = True
            if j != 0:
                if s_list[i][j - 1] == "#":
                    flg = True
            if j != w - 1:
                if s_list[i][j + 1] == "#":
                    flg = True
            if not flg:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

h,w = iim()

flag = True

l = []
dx = [0,0,1,-1]
dy = [1,-1,0,0]
for i in range(h):
    l.append(list(input()))

for i in range(h):
    for j in range(w):
        if l[i][j] == '#':
            check = False
            for k in range(4):
                if i+dx[k] < 0 or i+dx[k] >= h:
                    continue
                if j+dy[k] < 0 or j+dy[k] >= w:
                    continue
                if l[i+dx[k]][j+dy[k]] == '#':
                    check = True
            if not check:
                flag = False

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=list()
l.append(['.']*(w+2))
for i in range(h):
    l.append(['.']+list(input())+['.'])
l.append(['.']*(w+2))
p,pp=0,0
for i in range(1,h+1):
    for j in range(1,w+1):
        if l[i][j]=='#':
            p+=1
            pp+=int(l[i-1][j]=='#' or l[i+1][j]=='#' or l[i][j-1]=='#' or l[i][j+1]=='#')
print('NYoe s'[p==pp::2])

Submission status: Accepted

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

# #で上下左右隣接するマスに#がなければNo
s = []
for h in range(H):
    s.append(input())

for h in range(H):
    for w in range(W):
        if s[h][w] == '.':
            continue

        above = s[h - 1][w] if h >= 1 else '.'
        if above == '#':
            continue

        below = s[h + 1][w] if h < len(s) - 1 else '.'
        if below == '#':
            continue

        left = s[h][w - 1] if w >= 1 else '.'
        if left == '#':
            continue

        right = s[h][w + 1] if w < len(s[h]) - 1 else '.'
        if right == '#':
            continue

        print('No')
        exit(0)

print('Yes')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass

YES = "Yes"  # type: str
NO = "No"  # type: str


def solve(H: int, W: int, s: "List[str]"):
    print(YES if all(
        any(
            s[y + dy][x + dx] == "#"
            for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]
            if 0 <= x + dx < W and 0 <= y + dy < H
        )
        for y in range(H)
        for x in range(W)
        if s[y][x] == "#"
    ) else NO)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(H)]  # type: "List[str]"
    solve(H, W, s)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# solution

import io
import sys

nim,mike=map(int, input().split())

S = [input() for _ in range(nim)]

d = [-1,0,1,0,-1]

b = 0

for h in range(nim):
    for w in range(mike):
        if S[h][w] == ".": continue
        if any(S[h+x][w+y]=="#" for x,y in zip(d,d[1:]) if 0<=h+x<nim and 0<=w+y<mike):
            continue
        b = 1
print("YNeos"[b::2])

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

Submission status: Accepted

Solution code:
import sys
H, W = map(int, input().split())
s = [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):
        if s[i][j] == '#':
            for k in range(4):
                try:
                    if s[i + dy[k]][j + dx[k]] == '#':
                        flag = False
                        break
                    else:
                        flag = True
                except IndexError:
                    pass
            if flag:
                print('No')
                sys.exit()
print('Yes')

Submission status: Accepted

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

maze = [['.']*(w+2)] + [['.'] + list(input()) + ['.'] for i in range(h)] + [['.']*(w+2)]

def kuro(a,b):
    dx = [-1,1,0,0]
    dy = [0,0,-1,1]
    p = 0
    for i in range(4):
        if maze[a+dx[i]][b+dy[i]] == '#':
            p += 1
            maze[a+dx[i]][b+dy[i]] == '.'
    if p == 0:
        return False
    return True




for i in range(1,h+1):
    for j in range(1,w+1):
        if maze[i][j] == '#':
            if kuro(i,j) == False:
                print('No')
                break
    else:
        continue
    break
else:print('Yes')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
cam = [input() for i in range(H)]
ans = 'Yes'
for i in range(1, H-1):
	for j in range(1, W-1):
		if cam[i][j] == "#":
			if cam[i-1][j] == cam[i+1][j] == cam[i][j-1] == cam[i][j+1] == ".":
				ans = 'No'
				break
print(ans)

Submission status: Accepted

Solution code:
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

def main():
    h, w = nm()
    s = [['.'] * (w+1)]
    for _ in range(h):
        temp = ['.']
        temp.extend(list(ns()))
        temp.extend(['.'])
        s.append(temp)
    s.append(['.'] * (w+1))

    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')
    

if __name__ == '__main__':
    main()

Submission status: Accepted

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