Problem:


Score: 300 points

Problem Statement

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

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

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

Determine if square1001 can achieve his objective.  

Constraints

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

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

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

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

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

Sample Input 2

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

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

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

Sample Output 3

Yes



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


Sample output:
Yes

Submission status: Accepted

Solution code:
import numpy as np

H, W = map(int, input().split())
G = np.zeros((H+2, W+2))

for i in range(1, H+1):
    s = input()
    for j in range(1, W+1):
        if s[j-1] == "#":
            G[i, j] = 1

check = True

for i in range(H):
    for j in range(W):
        if G[i+1, j+1] == 0:
            continue

        up    = G[i  , j+1]
        right = G[i+1, j+2]
        down  = G[i+2, j+1]
        left  = G[i+1, j]
        if up + right + down + left == 0:
            check = False
            break

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

Submission status: Accepted

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

Submission status: Accepted

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

# s = [list(map(str, input().split())) for _ in range(h)]
s = [input() for _ in range(h)]

for i in range(h):
    for j in range(w):
        canPaint = 0
        if s[i][j] == '#':
            if i >= 1 and s[i - 1][j] == '#': # upper middle
                canPaint = 1
            if j >= 1 and s[i][j - 1] == '#': # middle left
                canPaint = 1
            if j <= w - 2 and s[i][j + 1] == '#': # middle right
                canPaint = 1
            if i <= h - 2 and s[i + 1][j] == '#': # lower middle
                canPaint = 1
            if not canPaint:
                print('No')
                quit()

print('Yes')

Submission status: Accepted

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


def main():
    import math
    import collections
    import itertools

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

    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if ss[i][j] == '#':
                if ss[i + 1][j] == '.' and ss[i - 1][j] == '.' and ss[i][j + 1] == '.' and ss[i][j - 1] == '.':
                    print("No")
                    break
        else:
            continue
        break
    else:
        print("Yes")



if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans='Yes'
for i in range(1, h+2):
    for j in range(1, 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]=='.':
            ans='No'
print(ans)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
a=[[False for j in range(W+2)] for i in range(H+2)]

for i in range(1,H+1):
    si=input()
    for j in range(1,W+1):
        a[i][j]=si[j-1]=='#'

f=True
fEND=False
for i in range(1,H+1):
    if fEND:
        break
    for j in range(1,W+1):
        if a[i][j] and not (a[i-1][j] or a[i+1][j] or a[i][j-1] or a[i][j+1]):
            f=False
            fEND=True
            break

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

Submission status: Accepted

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

shift = [[1,0],[-1,0],[0,-1],[0,1]]

flag = True
for j in range(H):
    for i in range(W):
        if s[j][i] == ".":
            continue
        else:
            flag = False
            for dx,dy in shift:
                nx = i + dx
                ny = j + dy
                
                if nx >= 0 and nx < W and ny >= 0 and ny < H:
                    if s[ny][nx] == "#":
                        flag = True
            if not flag:
                print("No")
                exit(0)

print("Yes")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
s = [input() for _ in range(H)]
A = "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] == ".":
            A = "No"
print(A)

Submission status: Accepted

Solution code:
import sys
h, w = map(int, input().split())
sq = []

for i in range(h):
    sq.append(input())

def is_valid(i, j):
#    global sq
    valid = False
    if i > 0 and sq[i-1][j] == '#':
        valid = True
    elif i < h-1 and sq[i+1][j] == '#':
        valid = True
    elif j > 0 and sq[i][j-1] == '#':
        valid = True
    elif j < w-1 and sq[i][j+1] == '#':
        valid = True
    return valid

for i in range(h):
    for j in range(w):
        c = sq[i][j]
        if c == '#':
            if not is_valid(i, j):
                print('No')
                sys.exit()

print('Yes')

Submission status: Accepted

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

dis=[[-1,0],[1,0],[0,-1],[0,1]]

for i in range(H):
    for j in range(W):
        if s[i][j]=='#':
            isfound=False
            for k in dis:
                if  0<=i+k[0]<H and 0<=j+k[1]<W:
                    if s[i+k[0]][j+k[1]]=='#' :
                        isfound=True
                else:
                    continue

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

print("Yes")

Submission status: Accepted

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

dis=[[-1,0],[1,0],[0,-1],[0,1]]

for i in range(H):
    for j in range(W):
        if s[i][j]=='#':
            isfound=False
            for k in dis:
                if  0<=i+k[0]<H and 0<=j+k[1]<W:
                    if s[i+k[0]][j+k[1]]=='#' :
                        isfound=True


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

print("Yes")

Submission status: Accepted

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

    mv = [[1,0],[-1,0],[0, 1],[0, -1]]
    for i in range(H):
        for j in range(W):
            if s[i][j] == "#":
                isfound = False
                for di, dj in mv:
                    i1, j1 = i+di, j+dj
                    if 0<=i1<H and 0<=j1<W and s[i1][j1] == "#":
                        isfound = True
                        break
                if not isfound: return "No"
    return "Yes"


if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

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

for i in range(H):
    s = input()
    b.append('.' + s + '.')

b.append('.' * (W + 2))

r = True

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if b[i][j] == '#':
            # print(i, j)
            if b[i][j-1] == '#':
                pass
            elif b[i][j+1] == '#':
                pass
            elif b[i-1][j] == '#':
                pass
            elif b[i+1][j] == '#':
                pass
            else:
                r = False

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[input() for i in range(H)]
ans=1
for i in range(H):
  for j in range(W):
    if(S[i][j]=='#'):
      if(i==0 and j==0):
        if(S[i+1][j]=='.' and S[i][j+1]=='.'):
          ans=0
      elif(i==0 and j==W-1):
        if(S[i+1][j]=='.' and S[i][j-1]=='.'):
          ans=0
      elif(i==0):
        if(S[i+1][j]=='.' and S[i][j+1]=='.' and S[i][j-1]=='.'):
          ans=0
      elif(i==H-1 and j==0):
        if(S[i-1][j]=='.' and S[i][j+1]=='.'):
          ans=0
      elif(i==H-1 and j==W-1):
        if(S[i-1][j]=='.' and S[i][j-1]=='.'):
          ans=0
      elif(i==H-1):
        if(S[i][j+1]=='.' and S[i-1][j]=='.' and S[i][j-1]=='.'):
          ans=0
      elif(j==0):
        if(S[i+1][j]=='.' and S[i][j+1]=='.' and S[i-1][j]=='.'):
          ans=0
      elif(j==W-1):
        if(S[i+1][j]=='.' and S[i-1][j]=='.' and S[i][j-1]=='.'):
          ans=0
      else:
        if(S[i+1][j]=='.' and S[i][j+1]=='.' and S[i-1][j]=='.' and S[i][j-1]=='.'):
            ans=0
if(ans):
  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):
        flag = True
        if S[i][j] == '#':
            for di, dj in zip([-1, 0, 1, 0], [0, -1, 0, 1]):
                if 0 <= i+di < h and 0 <= j+dj < w and S[i+di][j+dj] == '#':
                    flag = False
                    break
        else:
            flag = False
        if flag:
            break
    if flag:
        break

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

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
cambus = [list(input()) for _ in range(h)]
import sys
for i in range(h):
  for j in range(w):
    if cambus[i][j]=="#":
      b = []
      if i != 0:
        b.append(cambus[i-1][j] == "#")
      if i != h-1:
        b.append(cambus[i+1][j] == "#")
      if j != 0:
        b.append(cambus[i][j-1] == "#")
      if j != w-1:
        b.append(cambus[i][j+1] == "#")

      if not any(b):
        print("No")
        sys.exit()

print("Yes")

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

print('Yes')

Submission status: Accepted

Solution code:
from collections import deque
H,W = map(int, input().split())
m = [input() for _ in range(H)]
visited=[[0]*W for _ in range(H)]
dhw = [(1,0),(-1,0),(0,1),(0,-1)]
def main():
  global visited
  for h in range(H):
    for w in range(W):
      if m[h][w]==".":
        continue
      for dh, dw in dhw:
        nh, nw = h+dh, w+dw
        if 0<=nh<H and 0<=nw<W:
          if m[nh][nw]=="#":
            break
      else:
        print("No")
        return
  print("Yes")
  return

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
ans = "Yes"
c = ["0"]*(w+2)
s = []
s.append(c)
for i in range(h):
    S = ["0"]
    S1 = list(input())
    S1.append("0")
    S = S+S1
    s.append(S)
s.append(c)

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 ="No"
            else:
                continue
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

H,W = 0,0
Data = []

def check(x,y):
    if x > 0:
        if Data[y][x-1] == '#':
            return True
    if x < W-1:
        if Data[y][x+1] == '#':
            return True
    if y > 0:
        if Data[y-1][x] == '#':
            return True
    if y < H-1:
        if Data[y+1][x] == '#':
            return True
    return False 

H,W = map(int,input().split())
for _ in range(H):
    data = list(input())
    Data.append(data)

for y in range(H):
    for x in range(W):
        c = Data[y][x]
        if c == '#':
            if check(x,y) == False:
                print('No')
                sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=[list(map(str, input())) for i in range(h)]
for i in range(h):
    l[i]=['.']+l[i]+['.']
l=[['.']*(w+2)]+l+[['.']*(w+2)]
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+1][j]!='#' and l[i][j-1]!='#' and l[i][j+1]!='#':
            print('No')
            break
    else:
        continue
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
# ABC096
H, W = map(int, input().split())
S = [input()  for i in range(H)]
ans = "Yes"
# 4隅
if S[0][0] == "#":
    if S[0][1] == S[1][0] == ".":
        ans = "No"
if S[0][W-1] == "#":
    if S[0][W-2] == S[1][W-1] == ".":
        ans = "No"
if S[H-1][0] == "#":
    if S[H-2][0] == S[H-1][1] == ".":
        ans = "No"
if S[H-1][W-1] == "#":
    if S[H-2][W-1] == S[H-1][W-2] == ".":
        ans = "No"

# 角っこを除いた横辺
for w in range(1, W-1):
    if S[0][w] == "#":
        if S[0][w-1] == S[0][w+1] == S[1][w] == ".":
            ans = "No"
    if S[H-1][w] == "#":
        if S[H-1][w-1] == S[H-1][w+1] == S[H-2][w] == ".":
            ans = "No"

# 角っこを除いた縦辺
for h in range(1, H-1):
    if S[h][0] == "#":
        if S[h-1][0] == S[h][1] == S[h+1][0] == ".":
            ans = "No"
    if S[h][W-1] == "#":
        if S[h-1][W-1] == S[h][W-2] == S[h+1][W-1] == ".":
            ans = "No"

# 辺以外
for h in range(1, H-1):
    for w in range(1, W-1):
        if S[h][w] == "#":
            if S[h-1][w] == S[h][w-1] == S[h][w+1] == S[h+1][w] == ".":
                ans = "No"

print(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+1][j]=='.' and s[i][j+1]=='.' and s[i][j-1]=='.':
      ans='No'
print(ans)

Submission status: Accepted

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

def isBlack(i, j, h, w):
    if ((i-1>0 and s[max(i-1,0)][j] == 'B') or (j-1>0 and s[i][max(j-1,0)] == 'B') or (j+1<w-1 and s[i][min(j+1,w-1)] == 'B') or (i+1<h-1 and s[min(i+1,h-1)][j] == 'B')):
        return True
    else:
        return False

while (count != 0 and isCase):
    count = 0
    for i in range(h):
        for j in range(w):
            if (s[i][j] == '#'):
                s[i][j] = 'B'
                count += 1
                if (s[max(i-1,0)][j] == '#'): #上
                    s[max(i-1,0)][j] = 'B'
                elif (s[i][max(j-1,0)] == '#'): #左
                    s[i][max(j-1,0)] = 'B'
                elif (s[i][min(j+1,w-1)] == '#'): #右
                    s[i][min(j+1,w-1)] = 'B'
                elif (s[min(i+1,h-1)][j] == '#'): #下
                    s[min(i+1,h-1)][j] = "B"
                elif (isBlack(i, j, h ,w)):
                    pass
                else:
                    isCase = False
                    break
        if (isCase == False):
            break

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
tmp = '.' * (W + 2)
matrix = [tmp] * (H + 2)
for i in range(1,H+1):
    matrix[i] = '.' + input() + '.'
for i in range(1,H+1):
    for k in range(1,W+1):
        if matrix[i][k] == '.':
            continue
        elif matrix[i][k-1] == '.' and matrix[i][k+1] == '.' and matrix[i-1][k] == '.' and matrix[i+1][k] == '.':
            print('No')
            exit()
print('Yes')

Submission status: Accepted

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

for _ in range(h):
    rows.append(input())

for i in range(h):
    for j in range(w):
        if rows[i][j] =='#':
            judge = 0
            for nh,nw in [(1,0),(-1,0),(0,-1),(0,1)]:
                if (0<=i+nh<=h-1)and(0<=j+nw<=w-1):
                    if rows[i+nh][j+nw] =='#':
                        judge =1
                        break
            if judge == 0:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

for i in range(1, H):
    for j in range(1, W):
        if s[i][j] == '#':
            if i < H - 1 and j < W - 1:
                if '#' not in [s[i-1][j], s[i+1][j], s[i][j-1], s[i][j+1]]:
                    ng += 1
            elif i == H - 1 and j == W - 1:
                if '#' not in [s[i-1][j], s[i][j-1]]:
                    ng += 1
            elif j == W - 1:
                if '#' not in [s[i-1][j], s[i+1][j], s[i][j-1]]:
                    ng += 1
            elif i == H - 1:
                if '#' not in [s[i-1][j], s[i][j-1], s[i][j+1]]:
                    ng += 1
    if ng > 0:
        print('No')
        exit()

print('Yes')

Submission status: Accepted

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

for i in range(h):
    for k in range(w):
        if s[i][k] == "#":
            count = True
            d = [[0, 1], [1, 0], [0, -1], [-1, 0]]
            for l in d:
                if 0 <= i + l[0] < h and 0 <= k + l[1] < w:
                    if s[i + l[0]][k + l[1]] == "#":
                        count = False
                        break

            if count:
                ans = False

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

Submission status: Accepted

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

ans = 'Yes'
for hi in range(h):
  for wi in range(w):
    if field[hi][wi] == '#': 
      is_one = True
      for dw,dh in ((-1,0),(0,1),(1,0),(0,-1)):        
        if (0<= hi+dh < h) and (0<= wi+dw < w) :          
          if field[hi][wi] == field[hi+dh][wi+dw]:
            is_one = False      
      if is_one:
        ans ='No'
  
print(ans)

Submission status: Accepted

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

for i in range(1, h+1):
    for j in range(1, w+1):
        if m[i][j] == '#':
            if all(m[i+x][j+y] == '.' for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]):
                print('No')
                exit(0)
else:
    print('Yes')

Submission status: Accepted

Solution code:
def main():
  h,w=map(int,input().split())
  s=["*"*(w+2)]
  for i in range(h):
    s.append("*"+input()+"*")
  s+=["*"*(w+2)]
  a=[[0]*w for _ in range(h)]
  for i in range(h):
    for j in range(w):
      if s[i+1][j+1]!="#":
        a[i][j]=2
      else:
        a[i][j]=(s[i][j+1]+s[i+1][j:j+3]+s[i+2][j+1]).count("#")
  for i in a:
    if i.count(1)>0:
      print("No")
      return
  print("Yes")
main()

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if m[i][j] == '#':
            cnt = sum([0 <= i-1, i+1 < h, 0 <= j-1, j+1 < w])

            # up
            if 0 <= i-1:
                if m[i-1][j] != '#':
                    cnt -= 1

            # down
            if i+1 < h:
                if m[i+1][j] != '#':
                    cnt -= 1
            # left
            if 0 <= j-1:
                if m[i][j-1] != '#':
                    cnt -= 1

            # right
            if j+1 < w:
                if m[i][j+1] != '#':
                    cnt -= 1

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s_list = [list(input()) for _ in range(h)]
dx_list = [1, 0, -1, 0]
dy_list = [0, 1, 0, -1]
answer = 'Yes'
for i in range(h):
    for j in range(w):
        if s_list[i][j] == '#':
            num = 0
            for dx, dy in zip(dx_list, dy_list):
                x = j + dx
                y = i + dy
                if x < 0 or x >= w:
                    continue
                if y < 0 or y >= h:
                    continue
                if s_list[y][x] == '#':
                    num += 1
            if not num:
                answer = 'No'
print(answer)

Submission status: Accepted

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

f=True
for i in range(H):
	for j in range(W):
		if A[i][j]=='#':
			g=False
			for a,b in [(-1,0),(1,0),(0,1),(0,-1)]:
				if 0<=i+a<H and 0<=j+b<W:
					if A[i+a][j+b]=='#':g=True
			if not(g):f=False

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

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] == '#':
            isOK = False
            if i > 0 and S[i-1][j] == '#':
                isOK = True
            elif i < H-1 and S[i+1][j] == '#':
                isOK = True
            elif j > 0 and S[i][j-1] == '#':
                isOK = True
            elif j < W-1 and S[i][j+1] == '#':
                isOK = True
            if not isOK:
                break
    else:
        continue
    print('No')
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
field = [input() for _ in range(h)]
# 左、右、上、下
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
impossible = False

for y in range(h):
    if impossible:
        break

    for x in range(w):
        if field[y][x] == '.':
            continue

        check = False

        for i in range(4):
            nx = x+dx[i]
            ny = y+dy[i]

            if nx < 0 or nx >= w or ny < 0 or ny >= h:
                continue

            if field[ny][nx] == '#':
                check = True
        if not check:
            impossible = True
            break

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

Submission status: Accepted

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

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]=='.' 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:
# ABC096C - Grid Repainting 2
def main():
    # check all black grids' 4-dxy
    H, W, *S = open(0).read().split()
    H, W = int(H), int(W)
    dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    for x, s in enumerate(S):
        for y, a in enumerate(s):
            if a == "#":
                for dx, dy in dxy:
                    nx, ny = x + dx, y + dy
                    if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "#":
                        break
                else:
                    print("No")
                    return
    print("Yes")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


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


def main():
    H, W = map(int, input().split())
    s = [['.']*(W+2) for _ in range(H+2)]
    for i in range(H):
        s[i+1][1:-1] = list(input())

    # U L D R
    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 s[i][j] == '.':
                continue

            count = 0
            for x, y in zip(dx, dy):
                if s[i+y][j+x] == '.':
                    count += 1

            if count == 4:
                print('No')
                return

    print('Yes')


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
H,W=map(int,input().split())
nx=[1,0,-1,0]
ny=[0,1,0,-1]

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

for n in range(W):
  for m in range(H):
    if field[m][n]=='.':
      continue
    
    hope=0
    for j in range(4):
      x=n+nx[j]
      y=m+ny[j]
      
      if x<0 or W<=x or y<0 or H<=y:
        continue

      if field[y][x]=='#':
        hope=1
      
    if hope==0:
      print('No')
      exit()

print('Yes')

Submission status: Accepted

Solution code:
H,W=map(int, input().split())
S=["."+input()+"." for _ in range(H)]
S=["."*(W+2)]+S+["."*(W+2)]
ans="Yes"
for i in range(H):
  for j in range(W):
    if S[i][j]=="#" and 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:
H,W=[int(s) for s in input().split()]
cam_ls=[ "." for _ in range(H+2)]
for j in range(W+1):
  cam_ls[0]=cam_ls[0]+"."
  cam_ls[H+1]=cam_ls[H+1]+"."
for i in range(1,H+1):
  cam_ls[i]=cam_ls[i]+input()+"."
  
def pos(h,w):
  if cam_ls[h][w]==".":
    return 1
  elif (cam_ls[h+1][w]=="." and
        cam_ls[h-1][w]=="." and
        cam_ls[h][w+1]=="." and
        cam_ls[h][w-1]=="."):
    return 0
  else:
    return 1

ans=1
for i in range(1,H+1):
  for j in range(1,W+1):
    ans*=pos(i,j)
if ans==1:
  print("Yes")
else:
  print("No")

Submission status: Accepted

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

for i in range(1, h):
    for j in range(1, w):
        f = 0
        if board[i][j] == ".":
            continue
        for dy, dx in zip([0, -1, 1, 0], [-1, 0, 0, 1]):
            if board[i + dy][j + dx] == ".":
                f += 1
            if f == 4:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

arr=[]
for h in range(0,H):
    for w in range(0,W):
        if S[h][w]=='#':
            cnt=0
            if w!=0:
                cnt+=S[h][w-1]=='#'
            if w!=W-1:
                cnt+=S[h][w+1]=='#'
            if h!=0:
                cnt+=S[h-1][w]=='#'
            if h!=H-1:
                cnt+=S[h+1][w]=='#'
            arr.append(cnt>0)
print('Yes' if all(arr) else 'No')

Submission status: Accepted

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

for x in range(h):
    for y in range(w):
        if s[x][y] == "#":
            flag = False

            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] == "#":
                    flag = True

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

print("Yes")

Submission status: Accepted

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

def check() :
    for y in range(H) :
        for x in range(W) :
            if s[y][x] == '.' :
                continue
                
            for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)) :
                if not (0 <= (y + dy) < H and 0 <= (x + dx) < W) :
                    continue
                if s[y+dy][x+dx] == '#' :
                    break
            else :
                return 'No'
    return 'Yes'
    
print(check())

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(map(lambda x:x=="#", list(input())))for i in range(h)]
f=0
for i in range(h):
    for j in range(w):
        if s[i][j]:
            s[i][j] ^= 1
            if 1^(s[max(0,i-1)][j]or s[i][max(0,j-1)]or s[i][min(w-1,j+1)]or s[min(h-1,i+1)][j]):f=1
            s[i][j] ^= 1
print("YNeos"[f::2])

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
list1=[[0 for i in range(w)]for j in range(h)]
list2=[[0 for i in range(w)]for j in range(h)]
dx=[-1,1,0,0]
dy=[0,0,-1,1]
a=0
for j in range(h):
 s=input()
 for i in range(w):
  list1[j][i]=s[i]
for j in range(h):
  for i in range(w):
   if list1[j][i]=="#":
    for k in range(4):
     xk=i+dx[k]
     yk=j+dy[k]
     if xk>=0 and xk<w and yk>=0 and yk<h:
      list2[yk][xk]+=1
for j in range(h):
  for i in range(w):
    if list1[j][i]=="#" and list2[j][i]==0:
     None
    else:
     a+=1
if a==h*w:
 print("Yes")
else:
 print("No")

Submission status: Accepted

Solution code:
H,W = [int(x) for x in input().split()]
meiro = [list(input()) for _ in range(H)]
memo =[[0 for i in range(W)] for l in range(H)]
dx = [1,0,-1,0]
dy = [0,1,0,-1]
for i in range(H):
  for j in range(W):
    cnt = 0
    if meiro[i][j]=="#":
      for x,y in zip(dx,dy):
        if i+x<0 or W<=j+y or j+y<0 or H<=x+i or meiro[i+x][y+j]==".":
          cnt += 1
          if cnt==4:
            print("No")
            exit()
print("Yes")