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())
s = []
s.append(['.']*(w+2))
for i in range(1, h+1):
    t = input()
    u = ['.']
    for j in range(w):
        u.append(t[j])
    u.append('.')
    s.append(u)
s.append(['.']*(w+2))
key = True
d = [[0, -1], [0, 1], [-1, 0], [1, 0]]
flag = True
for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] == '#':
            key = False
            for k in range(len(d)):
                if s[i+d[k][0]][j+d[k][1]] == '#':
                    key = True
            if not key:
                flag = False
                break
    if not flag:
        break
if flag:
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

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

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

            flag = False
            for x, y in zip(dx, dy):
                if 0 <= x+w < W and 0 <= y+h < H and S[y+h][x+w] == '#':
                    flag = True
                    break

            if not flag:
                print('No')
                return

    print('Yes')
    
main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
D = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            # print('h, w', h, w)
            for dy, dx in D:
                ny = h + dy
                nx = w + dx
                if ny < 0 or ny < 0:
                    continue
                if nx >= W or ny >= H:
                    continue
                # print('ny, nx', ny, nx, S[ny][nx])
                if S[ny][nx] == '#':
                    break
            else:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

frag="True"
dxy=[(1,0),(0,1),(0,-1),(-1,0)]
for y in range(H):
    for x in range(W):
        if s[y][x]==".": continue
        c=0
        for dx,dy in dxy:
            if x+dx<0 or x+dx>W-1 or y+dy<0 or y+dy>H-1:
                c+=1
                continue
            if s[y+dy][x+dx]==".":
                c+=1
        if c==4:
            print("No")
            exit()
print("Yes")

Submission status: Accepted

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

s =[]
s_w =[]

for i in range(H):
    s_t = input()
    s_w =[]
    for j in range(W):           
        s_w.append(s_t[j])
    s.append(s_w)

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
        
        flag2 = False
        for k in range(len(dx)):
            ni = i + dy[k]
            nj = j + dx[k]
            if (ni<0 or H<=ni) or (nj<0 or W<=nj):
                continue
            
            #周りに一つも#がなければおわり
            if s[ni][nj] == "#":
                flag2 = True
                break
        
        if flag2 == False:
            flag = False
if flag == True:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [input() for _ in range(h)]
vec = {(-1, 0), (0, -1), (1,0), (0,1)}
for i in range(h):
  for j in range(w):
    if s[i][j] == '#':
      b = False
      for v in vec:
        ii = i+v[0]
        jj = j+v[1]
        if 0 <= ii <= h-1 and 0 <= jj <= w-1 and s[ii][jj] == '#':
          b = True
          break
      if not b:
        print('No')
        exit()
print('Yes')

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque

def dfs(ny,nx):
    d=deque([[ny,nx]])
    count=0
    while len(d)>0:
        now=d.pop()
        count+=1
        for dy,dx in (-1,0),(1,0),(0,-1),(0,1):
            y=now[0]+dy
            x=now[1]+dx
            if 0<=y<H and 0<=x<W:
                if flags[y][x]==False and grid[y][x]=="#":
                    d.append([y,x])
                flags[y][x]=True
    return count

#入力
readline=stdin.readline
H,W=map(int,readline().split())
grid=[]
for i in range(H):
    s=readline().strip()
    grid.append(s)

flags=[[False]*W for _ in range(H)]
answer=True
for y in range(H):
    for x in range(W):
        if flags[y][x]==True:
            continue
        elif grid[y][x]==".":
            flags[y][x]=True
        elif grid[y][x]=="#":
            flags[y][x]=True
            res=dfs(y,x)
            if res==1:
                answer=False
                break
    if answer==False:
        break

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [[i for i in input()] for j in range(h)]
flag = True
vecs = [(-1, 0), (0, 1), (1, 0), (0, -1)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            vec_cnt, w_cnt = 0, 0
            for di, dj in vecs:
                if 0 <= i + di < h and 0 <= j + dj < w:
                    vec_cnt += 1
                    if s[i + di][j + dj] == '.':
                        w_cnt += 1
            if vec_cnt == w_cnt:
                flag = False
print('Yes' if flag else 'No')

Submission status: Accepted

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

flag = True
for i in range(h):
    for j in range(w):
        now = canvas[i][j]
        if now == '#':
            left = canvas[i][max(0, j - 1)]
            right = canvas[i][min(w - 1, j + 1)]
            up = canvas[max(0, i - 1)][j]
            down = canvas[min(h - 1, i + 1)][j]
            if left == right == up == down == '.':
                flag = False

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

Submission status: Accepted

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

dxdy = [(0,-1), (-1,0), (0,1), (1,0)]
for y, row in enumerate(s):
    for x, e in enumerate(row):
        if e=='.':
            continue
        for dx, dy in dxdy:
            if not (0<=x+dx<w and 0<=y+dy<h):
                continue
            if s[y+dy][x+dx]=='#':
                break
        else:
            print('No')
            break
    else:
        continue
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
import numpy as np
H,W = map(int,input().split())
table = []
for i in range(H):
    col = list(input())
    table.append(col)
table = np.array(table)
for i in range(1,H):
    for j in range(1,W):
        bubun = table[i-1:i+2,j-1:j+2]
        if bubun.shape != (3,3):
            continue
        center = bubun[1,1]
        if center == "#" and bubun[0,1] == "." and bubun[1,0] == "." and bubun[1,2] == "." and bubun[2,1] == "." :
            print("No")
            exit()
        #print(bubun)
print("Yes")

Submission status: Accepted

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

    return H,W,S

def kansu(LH,LW,LS):
    success_flg  = True

    for h in range (LH):
        for w in range(LW):
            if LS[h][w] == '#':
                if h != 0:
                    if LS[h-1][w] == '#':
                        continue
                if h != LH-1:
                    if LS[h+1][w] == '#':
                        continue
                if w != 0:
                    if LS[h][w-1] == '#':
                        continue
                if W != LW-1:
                    if LS[h][w+1] == '#':
                        continue
                success_flg = False
                break
            

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




H,W,S = nyu()
kansu(H,W,S)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
S=[]
for i in range(h):
    s=input()
    S.append(s)
for i in range(1,h-1):
    for j in range(1,w-1):
        if S[i+1][j]==S[i][j+1]==S[i-1][j]==S[i][j-1]=="." and S[i][j]=="#":
            print("No")
            quit()
print("Yes")

Submission status: Accepted

Solution code:
A = [-1, 0, 0, 1]
B = [0, -1, 1, 0]


h, w = map(int, input().split())
GRID = ["." * (w + 2)] + ["." + input() + "." for _ in range(h)] + ["." * (w + 2)]

answer = "Yes"
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if GRID[i][j] == ".":
            continue
        flag = True
        for a, b in zip(A, B):
            if GRID[i + a][j + b] == "#":
                flag = False
        if flag:
            answer = "No"

print(answer)

Submission status: Accepted

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

for _ in range(h):
    c = ["."] + list(input()) + ["."]
    campus.append(c)

campus.append(["."] * (w + 2))

draw = True

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if campus[i][j] == "#":

            if campus[i - 1][j] == "." and\
               campus[i][j - 1] == "." and\
               campus[i + 1][j] == "." and\
               campus[i][j + 1] == ".":
               draw = False

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

Submission status: Accepted

Solution code:
#2次元配列の定義
H,W=input().split()
H=int(H)
W=int(W)
s = [[0 for c in range(W)] for r in range(H)]
#配列へ入力
for r in range(H):
    tempH=input()
    for c in range(W):
        s[r][c]=tempH[c]

for r in range(H):
    for c in range(W):
        if s[r][c]=='#': #黒塗りのマスなら、検証開始
            if r!=0:
                if s[r-1][c]=='#': #上のマスが黒塗りマスかどうか判定。
                    continue #このマスはOKなので、ループ続ける
            if r!=(H-1):
                if s[r+1][c]=='#': #下のマスが黒塗りマスかどうか判定。
                    continue #このマスはOKなので、ループ続ける
            if c!=0:
                if s[r][c-1]=='#': #左のマスが黒塗りマスかどうか判定。
                    continue #このマスはOKなので、ループ続ける
            if c!=(W-1):
                if s[r][c+1]=='#': #右のマスが黒塗りマスかどうか判定。
                    continue #このマスはOKなので、ループ続ける
            #途中でループを抜けられない=このマスはNG
            print('No')
            break
    else:
        continue
    break
else:
    #途中最後まで中断せずに検証できた=Yes判定
    print('Yes')

Submission status: Accepted

Solution code:
from itertools import product
H, W = list(map(int, input().split()))
S = [input() for _ in range(H)]
can = True
S = ['.' * (W + 2)] + ['.' + s + '.' for s in S] + ['.' * (W + 2)]
S = [list(s) for s in S]
dHW = [[-1, 0], [1, 0], [0, -1], [0, 1]]
for h, w in product(range(1, H + 1), range(1, W + 1)):
    if S[h][w] == '#':
        exist = False
        for (dh, dw) in dHW:
            if S[h + dh][w + dw] == '#':
                exist = True
                break
        if exist:
            continue
        else:
            can = False
            break
if can:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
h, w = map(int, input().split())
b = ".";j = [b+b*w+b]
s = j + [b+input()+b for _ in [0]*h] + j
for i in range(1, h+1):
    d = [n+1 for n in range(w) if s[i][n:n+3] == ".#."]
    if d and any([s[i-1][c] == s[i+1][c] == b for c in d]):
        print("No");exit()
print("Yes")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
l = ['.'*(w+2)]+list('.'+(input()+'.')for _ in range(h))+ ["."*(w+2)]
for i in range(1,h+1):
  for j in range(1,w+1):
    if l[i][j]=="#":
      if l[i-1][j]=="#" or l[i+1][j]=="#" or l[i][j+1]=="#" or l[i][j-1]=="#":
        continue
      else:
        print("No")
        exit()

print("Yes")

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    H, W = map(int, input().split())
    S = ['']* H
    for i in range(H):
        S[i] = input()
    delta = [1, -1]
    ans = 'Yes'
    for i in range(H):
        for j in range(W):
            if S[i][j] == '#':
                bl = False
                for d in delta:
                    if 0<=i+d<H and S[i+d][j]=='#':
                        bl = True
                    if 0<=j+d<W and S[i][j+d]=='#':
                        bl = True
                if bl == False:
                    ans = 'No'
    print(ans)







if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
s = np.array(s)
s[s == '#'] = 1
s[s == '.'] = 0
s = s.astype(np.int)
S = np.pad(s, [1, 1], 'constant')

ans = 'Yes'
for i in range(H):
    for j in range(W):
        if s[i, j] == 1:
            if np.sum(S[i:i+3, j+1]) + np.sum(S[i+1, j:j+3]) == 2:
                ans = 'No'
    if ans == 'No':
        break
                
print(ans)

Submission status: Accepted

Solution code:
H,W = list(map(int,input().split()))
table = []
for i in range(H):
    table.append(list(input()))
for i in range(H):
    for j in range(W):
        A = table[i][j]
        if A == "#":
            try:
                if table[i-1][j] == "#":
                    continue
            except IndexError:
                pass
            try:
                if table[i+1][j] == "#":
                    continue
            except IndexError:
                pass
            try:
                if table[i][j-1] == "#":
                    continue
            except IndexError:
                pass
            try:
                if table[i][j+1] == "#":
                    continue
            except IndexError:
                pass
            print("No")
            exit()
        else:
            continue
print("Yes")

Submission status: Accepted

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    H, W = list(map(int,_in[0].split(' ')))  # type:list(int)
    s_arr = []
    for i in range(H):
        _ = [_ for _ in _in[i+1]]   # type:list(str)
        s_arr.append(_)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    TF = 'Yes'
    for i in range(H):
        if TF == 'Yes':
            for j in range(W):
                if s_arr[i][j] == '#':
                    adjacent = []
                    if i>0:   
                        if s_arr[i-1][j] == '#':
                            continue
                    if i<H-1: 
                        if s_arr[i+1][j] == '#':
                            continue
                    if j>0:   
                        if s_arr[i][j-1] == '#':
                            continue
                    if j<W-1: 
                        if s_arr[i][j+1] == '#':
                            continue
                    TF = 'No'
                    break
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(TF)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    def main():
        H,W=map(int, input().split())
        grid=[list(input()) for i in range(H)]
        for h in range(H):
            for w in range(W):
                if grid[h][w]=='#':
                    for j,k in [0,1],[0,-1],[1,0],[-1,0]:
                        nh=h+j
                        nw=w+k
                        if nh<0 or nh>=H or nw<0 or nw>=W:
                            continue
                        else:
                            if grid[nh][nw]=='#':
                                break
                            else:
                                if [j,k]==[-1,0]:
                                    return 'No'
        return 'Yes'
    print(main())
resolve()

Submission status: Accepted

Solution code:
import sys
H, W = map(int, input().split())
li = []
li.extend(["."*(W+2)])
for i in range(H):
    li.extend(["."+input()+"."])
li.extend(["."*(W+2)])

for i in range(1,H+1):
    for j in range(1,W+1):
        if li[i][j] == "#":
            if li[i-1][j] == "." and li[i+1][j] == "." and li[i][j-1] == "." and li[i][j+1] == ".":
                print("No")
                sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
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):
        c=0
        if s[i][j]==".":
            continue
        for d in range(4):
            ni=i+dy[d]
            nj=j+dx[d]

            if ni<0 or h<=ni:
                continue
            if nj<0 or w <=nj:
                continue
            if s[ni][nj]=="#":
                c=1
        if c==0:
            print("No")
            exit()
print("Yes")

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop

import collections

h,w = map(int,input().split())
li = ["."*(w+2)]
for i in range(h):
    in_data = input()
    data = "." + in_data + "."
    li.append(data)
li.append("."*(w+2))

for i in range(1,h):
    for j in range(1,w):
        if li[i][j] == ".":
            continue
        if li[i-1][j] == "." and li[i+1][j] == "." and li[i][j-1] == "." and li[i][j+1] == ".":
            print("No")
            sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [list(input()) for i in range(H)]
dh = [-1, 0, 0, 1]
dw = [0, -1, 1, 0]
cnt = 0

for h in range(H):
  for w in range(W):
    # Check only black cell
    if s[h][w] == '.':
      continue
    for k in range(4):
      nh = h + dh[k]
      nw = w + dw[k]
      if nh < 0 or nw < 0 or H <= nh or W <= nw:
        continue
      # ok
      if s[nh][nw] == '#':
        break
    # failed
    else:
      print('No')
      break
  else:
    continue
  break
else:
  print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [list(input()) for i in range(H)]
dh = [-1, 0, 0, 1]
dw = [0, -1, 1, 0]
cnt = 0

for h in range(H):
  for w in range(W):
    # Check only black cell
    if s[h][w] == '.':
      continue
    for k in range(4):
      nh = h + dh[k]
      nw = w + dw[k]
      if nh < 0 or nw < 0 or H <= nh or W <= nw:
        continue
      # ok
      if s[nh][nw] == '#':
        break
    # failed
    else:
      cnt += 1

if cnt == 0:
  print('Yes')
else:
  print('No')

Submission status: Accepted

Solution code:
# Problem C - Grid Repainting 2

# input
H, W = map(int, input().split())
board = [[0]*W for i in range(H)]
for i in range(H):
    s_list = list(input())
    for j in range(W):
        board[i][j] = s_list[j]

# initialization
is_ok = True

# check
for i in range(H):
    for j in range(W):
        s = board[i][j]
        if s=='.':
            continue

        # right
        if j+1<W:
            if s=='#' and board[i][j+1]=='#':
                continue

        # left
        if j-1>=0:
            if s=='#' and board[i][j-1]=='#':
                continue

        # up
        if i-1>=0:
            if s=='#' and board[i-1][j]=='#':
                continue

        # down
        if i+1<H:
            if s=='#' and board[i+1][j]=='#':
                continue

        is_ok = False
        break

# output
if is_ok:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():

        H, W = map(int, input().split())
        grid = [[_ for _ in input()] for _ in range(H)]
        for h in range(H):
            for w in range(W):
                if grid[h][w]=='#':
                    for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]):
                        nh, nw = h + j, w + k
                        # nh と nw が街の範囲を超えてたら後の処理をスキップして別方向を模索
                        if nh < 0 or nh >= H or \
                                nw < 0 or nw >= W:
                            continue
                        else:
                            if grid[nh][nw] == '#':
                                break
                            else:
                                if [j, k] == [0, -1]:
                                    return 'No'
        return 'Yes'

    print(main())


resolve()

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 i > 0 and s[i-1][j] == "#":key = 1
      if i < h-1 and s[i+1][j] == "#":key = 1
      if j > 0 and s[i][j-1] == "#":key = 1
      if j < w-1 and s[i][j+1] == "#":key = 1
      if key == 0:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

Solution code:
import numpy as np
H,W=map(int, input().split())

S=[]
for h in range(H):
    s=list(input())
    S.append(s)

S=np.array(S)

d=[(1,0),(0,-1),(-1,0),(0,1)]
for h in range(H):
    for w in range(W):
        now=S[h,w]
        if now=="#":
            count=0
            for t in d:
                dx,dy=t
                nx=h+dx
                ny=w+dy
                if 0<=nx<H and 0<=ny<W:
                    if S[nx,ny]=="#":
                        count+=1
            if count==0:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
def main():
    H, W = (int(i) for i in input().split())
    A = [input() for i in range(H)]
    dhw = ((0, -1), (0, 1), (-1, 0), (1, 0))
    for h in range(H):
        for w in range(W):
            if A[h][w] == "#":
                for dh, dw in dhw:
                    if h+dh < 0 or H <= h+dh or w+dw < 0 or W <= w+dw:
                        continue
                    if A[h+dh][w+dw] == "#":
                        break
                else:
                    return print("No")
    print("Yes")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = list(input() for _ in range(h))
dxy = [(1,0),(-1,0),(0,1),(0,-1)]
for y in range(h):
    for x in range(w):
        if s[y][x] == ".": continue
        else:
            for dx,dy in dxy:
                nx,ny = x+dx,y+dy
                if not 0<=nx<w: continue
                if not 0<=ny<h: continue
                if s[ny][nx]=="#": break
            else:    
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
G = ["."*(W+2)]
for k in range(H):
    G.append("."+input()+".")
G.append("."*(W+2))

for h in range(1,H+1):
    for w in range(1,W+1):
        if G[h][w] == "#":
            if G[h-1][w] == "#" or G[h+1][w] == "#" or G[h][w-1] == "#" or G[h][w+1] == "#":
                pass
            else:
                print("No")
                exit(0)
print("Yes")

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
a = [list(input()) for _ in range(h)]
dx = [1,0,-1,0]
dy = [0,1,0,-1]
for k in range(h):
    for j in range(w):
        x,y = k,j
        if a[x][y] == "#":
            for i in range(4):
                xi,yi = x+dx[i],y+dy[i]
                if 0 <= xi < h and 0 <= yi < w and a[xi][yi] == "#":
                    break
            else:
                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]=='#':
            for ni,nj in ((i+1,j),(i-1,j),(i,j+1),(i,j-1)):
                if 0<=ni<H and 0<=nj<W and S[ni][nj]=='#':
                    break
            else:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

G = [['.'] * (W + 2)]
for _ in range(H):
    tmp = ['.'] + list(input()) + ['.']
    G.append(tmp)

tmp = ['.'] * (W + 2)
G.append(tmp)

move = [[1, 0], [-1, 0], [0, 1], [0, -1]]

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if G[i][j] == '#':
            flag = False
            for dx, dy in move:
                if G[i + dx][j + dy] == '#':
                    flag = True
                    break
            if not flag:
                print ('No')
                exit()

print ('Yes')

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = [input() for i in range(H)]
dxy = [(0,1),(1,0),(0,-1),(-1,0)]
for y in range(H):
    for x in range(W):
        if S[y][x] == '.': continue
        for dx,dy in dxy:
            nx,ny = x+dx,y+dy
            if not 0 <= nx < W: continue
            if not 0 <= ny < H: continue
            if S[ny][nx] == '#': break
        else:
            print('No')
            exit()
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
ok = True
for i in range(H):
  for j in range(W):
    if s[i][j] == "#":
      if i < H-1:
        if s[i+1][j] == "#": continue
      if i > 0:
        if s[i-1][j] == "#": continue
      if j < W-1:
        if s[i][j+1] == "#": continue
      if j > 0:
        if s[i][j-1] == "#": continue
      ok = False
      break
print("Yes" if ok else "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] == '#':
            flag = False
            for di,dj in (-1, 0), (1, 0), (0, 1), (0, -1):
                ni, nj = i+di, j+dj
                if 0 <= ni < h and 0 <= nj < w:
                    if S[ni][nj] == '#':
                        flag = True
            if not flag:
                print('No')
                exit()
else:
    print('Yes')

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, input().split())
    s = [["." for _ in range(w + 2)]]
    for _ in range(h):
        s.append(["."] + list(input()) + ["."])
    s.append(s[0])
    delta = [[0, 1], [1, 0], [0, -1], [-1, 0]]
    count_black = 0
    count_possible = 0
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if s[i][j] == "#":
                count_black += 1
                if any(s[dy + i][dx + j] == "#" for dy, dx in delta):
                    count_possible += 1
    print("Yes" if count_possible == count_black else "No")


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

flag = 1
for i in range(1, h+1):
    for j in range(1, w+1):
        f = 0
        if s[i][j] == "#":
            for x, y in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
                if s[i+x][j+y] == "#":
                    f = 1
            if f == 0:
                flag = 0

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

Submission status: Accepted

Solution code:
import sys
H, W = [int(x) for x in input().split()]
s = ['.' * (W + 2)] + ['.' + input() + '.' for _ in range(H)] + ['.' * (W + 2)]

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] == '.':
                print("No")
                sys.exit()
print("Yes")

Submission status: Accepted

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

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

grid = [input() for _ in range(h)]

def ok(y, x):
    for d, r in dir:
        if d + y < 0 or d + y >= h or r + x < 0 or r + x >= w:
            continue
        yield grid[d+y][r+x] == "#"

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

        if not any(ok(y, x)):
            print("No")
            exit()

print("Yes")

Submission status: Accepted

Solution code:
h, w = list(map(int, input().split()))
# b = list(map(int, input().split()))
s = []
s.append("."*(w+2))
for i in range(h):
    s.append("."+input()+".")
s.append("."*(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][j-1] == "." and s[i][j+1] == "." and s[i+1][j] == ".":
                print("No")
                exit()

print("Yes")