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())
board = [["." for x in range(W)]]
for i in range(H):
	board.append("." + input() + ".")
board.append(["." for x in range(W)])

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

Submission status: Accepted

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

mx = [0, 1, 0, -1]
my = [1, 0, -1, 0]
for i in range(h):
  for  j in range(w):
    if table[i][j] == "#":
      for k in range(4):
        tx, ty = j + mx[k], i + my[k]
        if 0 <= tx < w and 0 <= ty < h and table[ty][tx] == "#":
          break
      else:
        print("No")
        exit(0)
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] == ".":
            continue
        k = 0
        for x, y in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]:
            if x < 0 or x >= H:
                continue
            if y < 0 or y >= W:
                continue
            if S[x][y] == "#":
                k += 1
        if k == 0:
            print("No")
            exit()
print("Yes")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

# 入力
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]

cnt = 0
for i in range(0, H):
    for j in range(0, W):
        if i == 0 and j == 0 or i == 0 and j == W-1 or \
            i == H-1 and j == 0 or i == H-1 and j == W-1:
            continue
        if S[i][j] == "#":
            if i == 0:
                if S[i][j+1] != "#" and S[i][j-1] != "#" and S[i+1][j] != "#":
                    cnt += 1
            elif i == H-1:
                if S[i][j+1] != "#" and S[i][j-1] != "#" and S[i-1][j] != "#":
                    cnt += 1
            elif j == 0:
                if S[i][j+1] != "#" and S[i+1][j] != "#" and S[i-1][j] != "#":
                    cnt += 1
            elif j == W-1:
                if S[i][j-1] != "#" and S[i+1][j] != "#" and S[i-1][j] != "#":
                    cnt += 1
            else:
                if S[i][j+1] != "#" and S[i][j-1] != "#" \
                    and S[i+1][j] != "#" and S[i-1][j] != "#":
                    cnt += 1
if cnt == 0:
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

cnt = 0

if H >= 2 and W >= 3:
	if s[0][0] == "#" and s[0][1] == "." and s[1][0] == ".":
		cnt += 1

	for w in range(1, W - 1):
		if s[0][w] == "#" and s[0][w - 1] == "." and s[0][w + 1] == "." and s[1][w] == ".":
			cnt += 1

	if s[0][W - 1] == "#" and s[0][W - 2] == "." and s[1][W - 1] == ".":
		cnt += 1 

	if H >= 3:
		for h in range(1, H - 1):
			if s[h][0] == "#" and s[h][1] == "." and s[h + 1][0] == "." and s[h - 1][0] == ".":
				cnt += 1

			for w in range(1, W - 1):
				if s[h][w] == "#" and s[h][w - 1] == "." and s[h][w + 1] == "." and s[h + 1][w] == "." and s[h - 1][w] == ".":
					cnt += 1

			if s[h][W - 1] == "#" and s[h][W - 2] == "." and s[h + 1][W - 1] == "." and s[h - 1][W - 1] == ".":
				cnt += 1 

	if s[H - 1][0] == "#" and s[H - 1][1] == "." and s[H - 2][0] == ".":
		cnt += 1

	for w in range(1, W - 1):
		if s[H - 1][w] == "#" and s[H - 1][w - 1] == "." and s[H - 1][w + 1] == "." and s[H - 2][w] == ".":
			cnt += 1

	if s[H - 1][W - 1] == "#" and s[H - 1][W - 2] == "." and s[H - 2][W - 1] == ".":
		cnt += 1

elif H == 1:
	if s[0][0] == "#" and s[0][1] == ".":
		cnt += 1

	for w in range(1, W - 1):
		if s[0][w] == "#" and s[0][w - 1] == "." and s[0][w + 1] == ".":
			cnt += 1

	if s[0][W - 1] == "#" and s[0][W - 2] == ".":
		cnt += 1

elif W == 1:
	if s[0][0] == "#" and s[1][0] == ".":
		cnt += 1

	for h in range(1, H - 1):
		if s[h][0] == "#" and s[h - 1][0] == "." and s[h - 1][0] == ".":
			cnt += 1

	if s[H - 1][0] == "#" and s[H - 2][0] == ".":
		cnt += 1

else: #W==2
	if s[0][0] == "#" and s[0][1] == "." and s[1][0] == ".":
		cnt += 1

	if s[0][1] == "#" and s[0][0] == "." and s[1][1] == ".":
		cnt += 1

	if H >= 3:
		for h in range(1, H - 1):
			if s[h][0] == "#" and s[h][1] == "." and s[h + 1][0] == "." and s[h - 1][0] == ".":
				cnt += 1

			if s[h][1] == "#" and s[h][0] == "." and s[h + 1][1] == "." and s[h - 1][1] == ".":
				cnt += 1 

	if s[1][0] == "#" and s[1][1] == "." and s[0][0] == ".":
		cnt += 1

	if s[1][1] == "#" and s[1][0] == "." and s[0][1] == ".":
		cnt += 1


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

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
h, w = map(int, input().split())
s = np.array([list(input()) for _ in range(h)])
w1, w2 = np.array(["."]*h), np.array(["."]*(w+2))
s = np.hstack((w1.reshape(len(w1),1), s))
s = np.hstack((s, w1.reshape(len(w1),1)))
s = np.vstack((w2, s))
s = np.vstack((s, w2))
dx, dy = [1, 0, -1, 0], [0, 1, 0, -1]
for i in range(1, h+1, 1):
    for j in range(1, w+1, 1):
        now = s[i][j]
        c = 0
        if now == ".":
            continue
        for d in range(4):
            x = j + dx[d]
            y = i + dy[d]
            if now == s[y][x]:
                c += 1
        if c == 0:
            print("No")
            exit()
print("Yes")

Submission status: Accepted

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

judge = "Yes"
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            if i > 0 and i < h - 1:
                if s[i-1][j] == "#" or s[i+1][j] == "#":
                    continue
            if j > 0 and j < w - 1:
                if s[i][j-1] == "#" or s[i][j+1] == "#":
                    continue
            if i == 0:
                if s[i+1][j] == "#":
                    continue
            if j == 0:
                if s[i][j+1] == "#":
                    continue
            if i == h - 1:
                if s[i-1][j] == "#":
                    continue
            if j == w - 1:
                if s[i][j-1] == "#":
                    continue
            
            judge = "No"

print(judge)

Submission status: Accepted

Solution code:
import sys

def get_value_by_lists(values_list, pos):
    h, w = len(values_list), len(values_list[0])
    if 0 <= pos[1] < h and 0 <= pos[0] < w:
        return values_list[pos[1]][pos[0]]
    else:
        return None

def is_black_in_four_dir(cells_list, pos):
    for p in ((0, 1), (0, -1), (1, 0), (-1, 0)):
        if get_value_by_lists(cells_list, (pos[0]+p[0], pos[1]+p[1])) == "#":
            return True
    return False

h, w = map(lambda x: int(x), input().split(" "))
cells_list = [input() for _ in range(h)]

for y, cells in enumerate(cells_list):
    for x, cell in enumerate(cells):
        if cell == "#" and not (is_black_in_four_dir(cells_list, (x, y))):
            print("No")
            sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
import sys
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
t=[[(0 if i == 0 or j == 0 or i == (h+1) or j == (w+1) or s[i-1][j-1]=='.' else 1) for j in range(w+2)] for i in range(h+2)]
for i in range(1,h+1):
	for j in range(1,w+1):
		if t[i][j] == 0:
			continue
		if t[i-1][j] == 0 and t[i][j-1] == 0 and t[i][j+1] == 0 and t[i+1][j] == 0:
			print("No")
			sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
# 方針：黒く塗られるべきマスの隣（縦or横）に、同様のマスがあれば塗れる→Yes
# 逆に、黒く塗られるべきマスに接していない、黒く塗られるべきマスがあると、達成は不可能→No

# 行数・列数の入力(Row, Column)
H, W = list(map(int, input().split(" ")))

# 全入力を２次元配列へと変換
paint_map = []
for h in range(H):
    paint_map.append(list(input()))

# 各マス目の周りに黒があるかを確認する
ans = True  # 最終的な出力。始めは、塗ることが可能であるとして初期化してある

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

for i in range(H):
    for j in range(W):
        if paint_map[i][j] == "#":
            found = False  # 注目している黒マスの周りに、別の黒マスを見つけたか

            for k in range(4):
                ii = i + dx[k]
                jj = j + dy[k]

                if (H > ii >= 0) & (W > jj >= 0):  # 注目している座標がキャンパス上にあるなら
                    if paint_map[ii][jj] == "#":  # かつ黒色なら
                        found = True

            if not found:  # もしも注目している黒マスの周りに、別の黒マスが見つからなかったら
                ans = False

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

Submission status: Accepted

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

S = [list(map(lambda x: x == "#", list(input()))) for _ in range(H)]

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

ans = True
for j in range(H):
    for i in range(W):
        if S[j][i]:
            ok = False
            for x, y in zip(dx, dy):
                if i+x < 0 or i+x >= W or j+y < 0 or j+y >= H:
                    continue
                ok = ok or S[j+y][i+x]
            if not ok:
                ans = False
                break
        if not ans:
            break
    if not ans:
        break
print("Yes" if ans else "No")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
 
 
def enclose_field(field, outline="#"):
    """
    迷路等の外周をoutlineで埋めることで、IndexErrorを回避できます。
    [0, 0] → [1, 1]、[H-1, W-1] → [H, W]となります。
    """
    for i in range(len(field)):
        field[i] = [outline] + field[i] + [outline]
    field.append([outline] * (len(field[0])))
    field.insert(0, [outline] * (len(field[0])))
    return field
 
 
S = enclose_field(S, outline="a")
 
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            adj = [S[i+1][j], S[i-1][j], S[i][j+1], S[i][j-1]]
            if "#" not in adj:
                print("No")
                exit()
else:
    print("Yes")

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if picture[i][j] == "#":
            if i == 0:
                if j == 0:
                    if picture[i+1][j] != "#" and picture[i][j+1] != "#":
                        flag = False
                        break
                elif j == w-1:
                    if picture[i+1][j] != "#" and picture[i][j-1] != "#":
                        flag = False
                        break
                else:
                    if picture[i+1][j] != "#" and picture[i][j+1] != "#" and picture[i][j-1] != "#":
                        flag = False
                        break
            elif i == h-1:
                if j == 0:
                    if picture[i-1][j] != "#" and picture[i][j+1] != "#":
                        flag = False
                        break
                elif j == w-1:
                    if picture[i-1][j] != "#" and picture[i][j-1] != "#":
                        flag = False
                        break
                else:
                    if picture[i-1][j] != "#" and picture[i][j+1] != "#" and picture[i][j-1] != "#":
                        flag = False
                        break
            else:
                if j == 0:
                    if picture[i+1][j] != "#" and picture[i-1][j] != "#" and picture[i][j+1] != "#":
                        flag = False
                        break
                elif j == w-1:
                    if picture[i+1][j] != "#" and picture[i-1][j] != "#" and picture[i][j-1] != "#":
                        flag = False
                        break
                else:
                    if picture[i+1][j] != "#" and picture[i-1][j] != "#" and picture[i][j+1] != "#" and picture[i][j-1] != "#":
                        flag = False
                        break

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

Submission status: Accepted

Solution code:
def readQuestion():
    import sys
    h, w = map(int, sys.stdin.readline().split(' '))
    p = [[] for i in range(h)]
    for (i, line) in enumerate(sys.stdin):
        p[i] = list(map(lambda x:line[x] == '#', range(w)))
    return h, w, p

def mkNbr(s):
    nb = [[i-1,i+1] for i in range(s)]
    nb[0] = [1] if s > 1 else []
    nb[s-1] = [s-2] if s > 1 else []
    return nb

def solve(h, w, p):
    def hasNbr(i, j):
        return any((p[y][j] for y in nbH[i])) or \
               any((p[i][x] for x in nbW[j]))

    nbH = mkNbr(h)
    nbW = mkNbr(w)
    return 'Yes' if all((not p[i][j] or hasNbr(i,j)
                         for i in range(h) for j in range(w))) \
                 else 'No'

def main():
    h, w, p = readQuestion()
    print(solve(h, w, p))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    """
    1<=H,W<=50

    # or ## がない場合塗り方として無理
    #
    """
    H, W = map(int, input().split())

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

    f(H, W, s)


def f(H, W, s):
    d = list(zip(
        (0, 0, 1, -1),
        (1, -1, 0, 0)
    ))
    for h in range(H):
        for w in range(W):
            if s[h][w] == ".":
                continue

            ok = False
            for dy, dx in d:
                ny, nx = h+dy, w+dx
                if 0 <= ny < H and 0 <= nx < W and s[ny][nx] == "#":
                    ok = True
            if not ok:
                print("No")
                return

    print("Yes")


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

aaaaa=0
for i in range(h):
  for j in range(w):
     count=0

     if s[i][j]=="#":
        try:
            if s[i][j+1]=="#":
                count=1
        except: pass

        try:
            if s[i+1][j]=="#":
                count=1
        except: pass

        try:
            if s[i-1][j]=="#":
                count=1
        except: pass

        try:
            if s[i][j-1]=="#":
                count=1
        except: pass

        if count==0: aaaaa=1

if aaaaa==1:
    print("No")
else:
    print("Yes")

Submission status: Accepted

Solution code:
#abc096c
import sys

h,w=map(int,raw_input().split())
s=['.'*(w+2)]
for i in xrange(h):
 t='.'
 t+=raw_input()
 t+='.'
 s.append(t)
s.append('.'*(w+2))
for i in xrange(1,h+1):
 for j in xrange(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]=='#':
    continue
   else:
    print 'No'
    sys.exit()
print 'Yes'

Submission status: Accepted

Solution code:
def ok(h, w, grid):
    for r in range(h):
        for c in range(w):
            if grid[r][c] == '#':
                cnt = 0
                for dr, dc in [(1,0),(0,1),(-1,0),(0,-1)]:
                    nr, nc = r+dr, c+dc
                    if (0 <= nr < h) and (0 <= nc < w) and (grid[nr][nc] == '#'):
                        cnt += 1
                if cnt == 0:
                    return False
    return True
h, w = map(int, raw_input().split())
grid = [raw_input() for r in range(h)]
print "Yes" if ok(h, w, grid) else "No"

Submission status: Accepted

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


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

print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for i in range(H)]
dd = [(0, -1), (-1, 0), (0, 1), (1, 0)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            continue
        for dx, dy in dd:
            nx = j + dx; ny = i + dy
            if 0 <= nx < W and 0 <= ny < H:
                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 _ in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            continue
        if i-1 >= 0 and s[i-1][j] == '#':
            continue
        if i+1 <= h-1 and s[i+1][j] == '#':
            continue
        if j-1 >= 0 and s[i][j-1] == '#':
            continue
        if j+1 <= w-1 and s[i][j+1] == '#':
            continue
        print('No')
        break
    else:
      continue
    break
else:
    print('Yes')

Submission status: Accepted

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

for i in range(H):
    s[i].append(".")
    s[i].insert(0,".")

add_list = ["." for _ in range(W+2)]
s.append(add_list)
s.insert(0,add_list)

Flag = "Yes"

for i in range(H):
    for j in range(W):
        if (s[i][j]=="#" and (s[i][j+1]=="." and s[i][j-1]=="." and s[i+1][j]=="." and s[i-1][j]==".")):
            Flag = "No"
            break
print(Flag)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
grid = []
ans = "Yes"
for i in range(h):
    grid.append(list(input()))
command = [(-1, 0), (1, 0), (0, -1), (0, 1)]

for x in range(h):
    for y in range(w):
        if grid[x][y] == "#":
            flag = "No"
            for m, n in command:
                try:
                    if grid[x + m][y + n] == "#":
                        flag = "Yes"
                except:
                    None
            if flag == "No":
                ans = flag
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
t=[list(input())for i in range(h)]
ans=True
dx=[1,-1,0,0]
dy=[0,0,1,-1]
for i in range(h):
    for j in range(w):
        if t[i][j]=="#":
            found=False#隣あう黒マスが見つかったか判定する
            for k in range(4):
                i2=i+dx[k]
                j2=j+dy[k]
                if 0<=i2 and i2<h and 0<=j2 and j2<w and t[i2][j2]=="#":
                    found=True
            if found==False:
                ans=False
if ans:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = []
for i in range(H):
    s.append(input())
for y in range(H-1):
    for x in range(W):
        if(s[y][x] == "#"):
            if(s[y-1][x] == "#" or s[y+1][x] == "#" or s[y][x-1] == "#" or s[y][x+1] == "#"):
                pass
            else:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = []
flag = "Yes"
for i in range(0,H):
    S.append(list(input()))

    
for i in range(0,H):
    for j in range(0,W):
        if S[i][j] == "#":
            if i != 0 and S[i-1][j] == "#" : continue
            elif i != H-1 and S[i+1][j] == "#": continue
            elif j != 0 and S[i][j-1] == "#": continue
            elif j != W-1 and S[i][j+1] == "#":continue
            else: flag = "No"
            
print(flag)

Submission status: Accepted

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

c = [[-1]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            c[i][j] += 1
            if i != 0: c[i][j] += (S[i-1][j] == '#')
            if i != H-1: c[i][j] += (S[i+1][j] == '#')
            if j != 0: c[i][j] += (S[i][j-1] == '#')
            if j != W-1: c[i][j] += (S[i][j+1] == '#')

if any(list(map(lambda s: 0 in s, c))):
    print('No')
else:
    print('Yes')

Submission status: Accepted

Solution code:
import sys

h, w = map(int, input().split())

array = ["".join(list(input())) for x in range(h)]

flag = True
for h_i in range(1, len(array)-2):
    for w_i in range(1, len(array[h_i]) - 2):
        if array[h_i][w_i] == "#" and array[h_i+1][w_i] == "." and array[h_i][w_i+1] == "." and \
                array[h_i - 1][w_i] == "." and "." == array[h_i][w_i - 1]:
            print("No")
            sys.exit()

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - Grid Repainting 2
https://abc096.contest.atcoder.jp/tasks/abc096_c

"""
import sys
from sys import stdin
input = stdin.readline


def is_possible(h, w, pict):
    moves = [(0, -1), (0, 1), (-1, 0), (1, 0)]
    check = True

    ans = [['.'] * w for _ in range(h)]
    for cy in range(h):
        for cx in range(w):
            if ans[cy][cx] != pict[cy][cx]:
                check = False
                for dxy in moves:
                    nx = cx + dxy[0]
                    ny = cy + dxy[1]

                    if 0<= nx <= w-1 and 0 <= ny <= h-1:
                        if pict[ny][nx] == '#':
                            ans[ny][nx] = '#'
                            check = True
                            break
                if check != True:
                    return 'No'
    return 'Yes'


def main(args):
    h, w = map(int, input().split())
    pict = []
    for _ in range(h):
        pict.append(list(input().strip()))

    ans = is_possible(h, w, pict)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# coding : utf-8

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

ans = 'Yes'
endFlg = False
# '#'の点をみつけたときに、その上下左右に'#'がない場合、孤立した点を黒く塗ることはできないので、答えはNoとなる。
# 全部の'#'の点について、その上下左右のいずれかに'#'が見つかれば、答えはYesとなる。

for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            if i != 0 and S[i-1][j] == '#':
                continue
            elif i != H-1 and S[i+1][j] == '#':
                continue
            elif j != 0 and S[i][j-1] == '#':
                continue
            elif j != W-1 and S[i][j+1] == '#':
                continue
            else :
                ans = 'No'
                endFlg = True
                break
    if endFlg:
        break

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

# 入力を各変数に格納
x, y = map(int, input().split())

# キャンバスを2次元配列に格納
canvas = []
for i in range(x):
	canvas.append(list(input()))

for i in range(x):
	for j in range(y):
		# 対象マスが#の時はチェックする
		if canvas[i][j] == "#":
			# 左端以外
			if i != 0:
				# 左隣
				if canvas[i-1][j] == "#":
					continue
			# 右端以外
			if i != x-1:
				# 右隣
				if canvas[i+1][j] == "#":
					continue
			# 最上段以外
			if j != 0:
				# ひとつ上
				if canvas[i][j-1] == "#":
					continue
			# 最下段以外
			if j != y-1:
				# ひとつ下
				if canvas[i][j+1] == "#":
					continue
			# どの条件も当てはまらない→四方に#がない
			print("No")
			exit()
# 一度もNoを通らなければYes
print("Yes")

Submission status: Accepted

Solution code:
l = input().split()
h,w = int(l[0]),int(l[1])
mapxy = []
for i in range(h):
    mapxy.append(list(input()))
dx = [1,0,-1,0]
dy = [0,1,0,-1]
flag1 = 0
flag2 = 0
for i in range(h):
    for j in range(w):
        if mapxy[i][j] == "#":
            for k in range(4):
                if 0<=i+dy[k]<h and 0<=j+dx[k]<w:
                    if mapxy[i+dy[k]][j+dx[k]] == "#":
                        flag1 = 1
            if flag1 == 0:
                flag2 = 1
                break
            flag1 = 0
    if flag2 == 1:
        print("No")
        break
if flag2 != 1:
    print("Yes")

Submission status: Accepted

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

field = []
for i in range(h):
    field.append(input())
flag = "Yes"

for i in range(h):
    for j in range(w):
        if field[i][j] is "#":
            if i != 0 and field[i-1][j] is "#": continue
            elif i != h-1 and field[i+1][j] is "#": continue
            elif j != 0 and field[i][j-1] is "#": continue
            elif j != w-1 and field[i][j+1] is "#": continue
            else: flag = "No"
print(flag)

Submission status: Accepted

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

mx = [0, 1, 0, -1]
my = [1, 0, -1, 0]
for i in range(h):
  for  j in range(w):
    if table[i][j] == "#":
      for k in range(4):
        tx, ty = j + mx[k], i + my[k]
        if 0 <= tx < w and 0 <= ty < h and table[ty][tx] == "#":
          visited[i][j] = visited[ty][tx] = "#"

for i in range(h):
  for  j in range(w):
    if table[i][j] != visited[i][j]:
      print("No")
      exit(0)
print("Yes")

Submission status: Accepted

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

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

ans = True
for i in range(h):
    for j in range(w):
        if (s[i][j] == '#'):
            found = False
            for k in range(4):
                i2 = i + dx[k]
                j2 = j + dy[k]
                if (0 <= i2 < h) and (0 <= j2 < w) and (s[i2][j2] == '#'):
                    found = True
            if not(found):
                ans = False

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

Submission status: Accepted

Solution code:
def c_grid_repainting(H, W, Grid):
    delta = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    for h in range(H):
        for w in range(W):
            if Grid[h][w] == '.':  # 白マスは無視
                continue
            black = 0  # マス(h,w)に隣り合った黒マスの数
            for dh, dw in delta:
                is_internal = 0 <= h + dh < H and 0 <= w + dw < W
                if is_internal and Grid[h + dh][w + dw] == '#':
                    # 隣のマスはキャンバスの内部で、黒だった
                    black += 1
            if black == 0:
                # 黒マス候補の隣に黒マスがなかった。そのマスは塗れない
                return 'No'
    return 'Yes'

H,W = [int(i) for i in input().split()]
Grid = [input().strip() for _ in range(H)]
print(c_grid_repainting(H, W, Grid))

Submission status: Accepted

Solution code:
import numpy as np

h,w=list(map(int,input().split()))
s=np.zeros((h+2,w+2))

for i in range(1,h+1):
    input_=input()
    for j in range(1,w+1):
        if input_[j-1]=="#":
            s[i][j]=1
n=0
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j]==1:
            if (s[i-1][j]==0 and s[i+1][j]==0 and s[i][j+1]==0 and s[i][j-1]==0):
                n+=1
if n==0:
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

s = [raw_input() for _ in range(H)]

ans = "Yes"

for y in range(H):
  for x in range(W):
    if s[y][x]=="#":
      ok = False
      for dx,dy in [(-1,0), (1,0), (0,-1), (0,1)]:
        if 0<=x+dx<W and 0<=y+dy<H and s[y+dy][x+dx]=="#":
          ok = True
      if not ok:
        ans = "No"

                
print ans

Submission status: Accepted

Solution code:
from itertools import repeat, product

def get_targets(a_grid):
    h = len(a_grid)
    w = len(a_grid[0])

    targets = [t for t in product(range(h), range(w)) if a_grid[t[0]][t[1]] == '#']

    return targets

def get_arounds(t, h, w):
    arounds = map(lambda a: (t[0] + a[0], t[1] + a[1]), ((0, 1), (1, 0), (0, -1), (-1, 0)))

    return list(filter(lambda x: -1 < x[0] < h and -1 < x[1] < w, arounds))

def main():
    h, w = tuple(map(int, input().split()))
    a_grid = [list(input()) for _ in range(h)]

    black_sq = get_targets(a_grid)

    if all([any([(sq in black_sq) for sq in get_arounds(bsq, h, w)]) for bsq in black_sq]):
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

paintable = True

for i in range(H):
    for j in range(W):
        if grid[i][j] == '#':
            if i != 0:
                if grid[i-1][j] == '#':
                    continue
            if i != H - 1:
                if grid[i+1][j] == '#':
                    continue
            if j != 0:
                if grid[i][j-1] == '#':
                    continue
            if j != W - 1:
                if grid[i][j+1] == '#':
                    continue
            paintable = False
    if not paintable:
        break
        
print("Yes" if paintable else "No")