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:
hw_list = list(map(int, input().split()))
h = hw_list[0]
w = hw_list[1]
row_list = ['.'*(w+2)]

def impossible_check(area, x, y):
    impossible = True
    if area[y-1][x] == '#':
        impossible = False
    if area[y][x-1] == '#':
        impossible = False
    if area[y][x+1] == '#':
        impossible = False
    if area[y+1][x] == '#':
        impossible = False
    return impossible

for i in range(h):
    row_list.append('.'+ input()+ '.')
row_list.append('.'*(w+2))

for y in range(1, h+1):
    for x in range(1, w+1):
        if row_list[y][x] == '#' and impossible_check(row_list, x, y):
            print('No')
            exit()

print('Yes')

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

#print(S)
    

no=0
for i in range(h):
    for j in range(w):
        #no=0
        if S[i][j]=="#" and S[i][j-1]=="." and S[i][j+1]=="." and S[i-1][j]=="." and S[i+1][j]=="." :
            no=1

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

Submission status: Accepted

Solution code:
H,W=list(map(int,input().split()))
img=[[0 for _ in range(W+2)] for _ in range(H+2)]
for y in range(H):
    for x,c in enumerate(input()):
        if c=='#':
            img[y+1][x+1]=1
ans='Yes'
for y in range(1,H+1):
    for x in range(1,W+1):
        if img[y+1][x+1]==1:
            if img[y][x+1]+img[y+2][x+1]+img[y+1][x+2]+img[y+1][x]==0:
                ans='No'
                break
    if ans=='No':
        break
print(ans)

Submission status: Accepted

Solution code:
def is_success(s, h, w):
    for i in range(h):
        for j in range(w):
            if s[i][j] != '#':
                continue
            if i >= 1 and s[i-1][j] == '#':
                continue
            if i <= h-2 and s[i+1][j] == '#':
                continue
            if j >= 1 and s[i][j-1] == '#':
                continue
            if j <= w-2 and s[i][j+1] == '#':
                continue
            return 'No'

    return 'Yes'


h, w = [int(v) for v in input().split()]
s = []
for i in range(h):
    s.append(input())

print(is_success(s, h, w))

Submission status: Accepted

Solution code:
height, width = map(int, input().split())

matrix = []
for h in range(height):
    matrix.append(input())

for h in range(height):
    for w in range(width):
        
        able = False
        if matrix[h][w] == "#":
            try:
                if matrix[h+1][w] == "#":
                    able = True
            except:
                pass
            
            try:
                if matrix[h-1][w] == "#":
                    able = True
            except:
                pass
            
            try:
                if matrix[h][w+1] == "#":
                    able = True
            except:
                pass
            
            try:
                if matrix[h][w-1] == "#":
                    able = True
            except:
                pass
        
            if able is False:
                print("No")
                exit(0)

print("Yes")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
s = []
answer = "Yes"

for i in range(H):
    s.append(str(input()))

for i in range(H):
    for j in range(W):
        if s[i][j] == "#":
            if j != 0 and s[i][j-1] == ".":
                if j+1 != W and s[i][j+1] == ".":
                    if i != 0 and s[i-1][j] == ".":
                        if i+1 != H and s[i+1][j] == ".":
                            answer ="No"

print(answer)

Submission status: Accepted

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

def search(i,j):
    c = 0

    if i != 0:
        if ss[i-1][j] == "#":
            c += 1
    if i != h-1:
        if ss[i+1][j] == "#":
            c += 1
    if j != 0:
        if ss[i][j-1] == "#":
            c += 1
    if j != w-1:
        if ss[i][j+1] == "#":
            c += 1

    if c > 0:
        return False
    else:
        return True

ans = "Yes" 
for i in range(h):
    for j in range(w):
        if ss[i][j] == "#":
            if search(i,j):
                ans = "No"
                break

print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
ma = []
ma.append(["."]*(w+2))
for i in range(h):
    k = ["."]
    k += input()
    k += "."
    ma.append(k)
ma.append(["."] * (w + 2))
end = 0
for i in range(h):
    if end == 1:
        break
    for j in range(w):
        if ma[i + 1][j + 1] == "#":
            if ma[i][j + 1] == "." and ma[i + 1][j] == "." and ma[i + 2][j + 1] == "." and ma[i + 1][j + 2] == ".":
                print("No")
                end = 1
                break
else:
    print("Yes")

Submission status: Accepted

Solution code:
import sys

h,w = map(int,input().split())
C = [input() for _ in range(h)]

dd = [(0,1),(0,-1),(1,0),(-1,0)]
for i in range(h):
    for j in range(w):
        if C[i][j] == '#':
            b = []
            for di,dj in dd:
                if 0<=i+di<h and 0<=j+dj<w:
                    b.append(C[i+di][j+dj] == '.')
            if all(b):
                print('No')
                sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
from sys import stderr
from functools import reduce
from operator import add
def f(): return [int(i) for i in input().split()]
def yes(n): print("Yes" if n else "No")
def debug(*x, sep=" ", end="\n"):
    for item in x:
        stderr.write(str(item))
        stderr.write(sep)
    stderr.write(end)

h, w = f()
target =  ['.'*(w+2)]
for _ in range(h):
    a = '.'
    a += input()
    a += '.'
    target.append(a)
target.append('.'*(w+2))
flag = True

for i in range(1, h+1):
    for j in range(1, w+1):
        if target[i][j] == '#':
            if '#' not in [target[i-1][j], target[i+1][j], target[i][j-1], target[i][j+1]]:
                flag = False
yes(flag)

Submission status: Accepted

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

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

for y in range(1, H+1):
    for x in range(1, W+1):
        if s[y][x] == "#":
            if s[y-1][x] == "." and s[y+1][x] == "." and s[y][x-1] == "." and s[y][x+1] == ".":
                accept = False

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000)

H, W = list(map(int, input().split()))
s = [[c for c in input()] for _ in range(H)]
accept = True

def dfs(x, y, count):
    if  x < 0 or W <= x or y < 0 or H <= y:
        return count - 1
    elif s[y][x] != "#":
        return count - 1
    else:
        s[y][x] = "."
        return max(dfs(x-1, y, count+1), dfs(x+1, y, count+1), dfs(x, y-1, count+1), dfs(x, y+1, count+1))

for y in range(H):
    for x in range(W):
        if s[y][x] != "#":
            continue
        elif dfs(x, y, 0) == 0:
            accept = False

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

Submission status: Accepted

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

# for i in range(h):
#     print("".join(map(str, T[i])))

flag = True
for i in range(h):
    for j in range(w):
        if T[i][j] == "#":
            flag2 = False
            for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
                nx, ny = i + dx, j + dy

                if 0 <= nx < h and 0 <= ny < w and T[nx][ny] == "#":
                    flag2 = True

            if not flag2:
                flag = False

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

Submission status: Accepted

Solution code:
H, W =list(map(int, input().split()))
X = []
def neibour(x,y):
    try:
        if X[x][y] == '#':
            return 1
        else:
            return 0
    except:
        return 0
for i in range(H):
    X.append(list(input()))
for i in range(H):
    for j in range(W):
        if X[i][j] == '#':
            c = 0
            c += neibour(i+1,j)
            c += neibour(i-1,j)
            c += neibour(i,j+1)
            c += neibour(i,j-1)
            if c == 0:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

slist = []
slist.append(["." for j in range(W+2)] )
for i in range(H):
    l = [j for j in raw_input()]
    l.insert(0, ".")
    l.append(".")
    slist.append(l)
slist.append(["." for j in range(W+2)])
# for s in slist:
#     print(s)

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

Submission status: Accepted

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

for i in range(H):
	for j in range(W):
		if picture[i][j] == '#':
			if i == 0 or picture[i-1][j] == '.':
				if j == 0 or picture[i][j-1] == '.':
					if i == H-1 or picture[i+1][j] == '.':
						if j == W-1 or picture[i][j+1] == '.':
							print('No')
							exit()

print('Yes')

Submission status: Accepted

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

Submission status: Accepted

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


def f(h, w):
    if h < 0 or h >= H or w < 0 or w >= W:
        return False
    return S[h][w] == '#'


for i in range(H):
    for j in range(W):
        if f(i, j) and not f(i-1, j) and not f(i+1, j) and not f(i, j-1) and not f(i, j+1):
            print('No')
            exit()

print('Yes')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
s=[list(input()) for i in range(H)]
dx=[0,0,-1,1]
dy=[1,-1,0,0]
for i in range(H):
    for j in range(W):
        if s[i][j]=="#":
            flag=0
            for k in range(4):
                x=i+dx[k]
                y=j+dy[k]
                if 0<=x<H and 0<=y<W:
                    if s[x][y]=="#":
                        flag=1
            if flag==0:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
import sys

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

s=[["" for i in range(w)] for j in range(h)]

for i in range(h):
    a=input()
    for j in range(w):
        s[i][j]=a[j]
        
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            if i != 0:
                if s[i-1][j] == "#":
                    continue
            if i != h-1:
                if s[i+1][j] == "#":
                    continue
            if j != 0:
                if s[i][j-1] == "#":
                    continue
            if j != w-1:
                if s[i][j+1] == "#":
                    continue
            print("No")
            sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
# coding: utf-8

h,w = list(map(int,input().split()))
m = []
for i in range(h):
    m.append(input())

for i in range(h):
    for j in range(w):
        t,r,l,b = [""]*4
        if m[i][j] == "#":
            if i-1 >= 0:
                t = m[i-1][j]
            if j+1 < w:
                r = m[i][j+1]
            if j-1 >= 0:
                l = m[i][j-1]
            if i+1 < h:
                b = m[i+1][j]
            if [t,r,l,b].count("#") == 0:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

flag = True

for i in range(0, H):
	for j in range(0, W):
		if board[i][j] == '#':
			tmp = False
			if i-1 >= 0:
				if board[i-1][j] == '#':
					tmp = True
			if j-1 >= 0:
				if board[i][j-1] == '#':
					tmp = True
			if i+1 <= H-1:
				if board[i+1][j] == '#':
					tmp = True
			if j+1 <= W-1:
				if board[i][j+1] == '#':
					tmp = True
			flag = flag and tmp

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

Submission status: Accepted

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

s = [input() for i in range(h)]

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

for j in range(1,w-1):
    if s[0][j] == "#" and s[1][j] == "." and s[0][j + 1] == "." and s[0][j - 1] == ".":
        print("No")
        exit()
    if s[h-1][j] == "#" and s[h-2][j] == "." and s[h-1][j + 1] == "." and s[h-1][j - 1] == ".":
        print("No")
        exit()


for i in range(1,h-1):
    if s[i][0] == "#" and s[i + 1][0] == "." and s[i][1] == "." and s[i - 1][0] == ".":
        print("No")
        exit()
    if s[i][w-1] == "#" and s[i + 1][w-1] == "." and s[i][w-2] == "." and s[i - 1][w-1] == ".":
        print("No")
        exit()

if s[0][0]=="#" and s[1][0]=="." and s[0][1]==".":
    print("No")
    exit()

if s[h-1][0]=="#" and s[h-1][1]=="." and s[h-2][0]==".":
    print("No")
    exit()

if s[0][w-1]=="#" and s[1][w-1]=="." and s[0][w-2]==".":
    print("No")
    exit()

if s[h-1][w-1]=="#" and s[h-1][w-2]=="." and s[h-2][w-1]==".":
    print("No")
    exit()

print("Yes")

Submission status: Accepted

Solution code:
import sys

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

# H = 2
# W = 3

s = [["" for j in range(W)] for i in range(H)]

for i in range(H):
    tmp = input()
    for j in range(W):
        s[i][j] = tmp[j]

# print(s)

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

Submission status: Accepted

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

grid = [['.' for i in range(0, W+2)] for i in range(0, H+2)]
temp = []

for i in range(1, H+1):
    temp.append(list(input()))
    grid[i][1:1+W] = temp[i-1][::]
    
ans = 'Yes'
    
for i in range(1, H+1):
    for j in range(1, W+1):
        if grid[i][j] == '#':
            if grid[i+1][j] == '#' or grid[i-1][j] == '#':
                pass
            elif grid[i][j+1] == '#' or grid[i][j-1] == '#':
                pass
            else:
                ans = 'No'

print(ans)

Submission status: Accepted

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

S = [None] * H

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

flag_break = False

for h in range(H):
    if flag_break == False:
        for w in range(W):
            if S[h][w] == '#':
                if S[max(0, h-1)][w] != '#' and S[h][max(0, w-1)] != '#' and S[h][min(W-1, w+1)] != '#' and S[min(H-1, h+1)][w] != '#':
                    flag_break = True
                    break

if flag_break == True:
    print('No')
else:
    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 and S[ny][nx] == '#':
        break
    else:
      print('No')
      quit()
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(input())
dx = [-1, 0, 0, 1]
dy = [0, 1, -1, 0]
isOK = True
for i in range(H):
    for j in range(W):
        if(S[i][j] == '#'):
            isOK2 = False
            for k in range(4):
                if(0 <= i + dy[k] < H and 0 <= j + dx[k] < W):
                    if(S[i + dy[k]][j + dx[k]] == '#'):
                        isOK2 = True
            if(isOK2 == False):
                isOK = False
if(isOK):
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

ans_flg = True
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            flg = False
            for dr, dc in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
                r = i + dr
                c = j + dc
                if 0 <= r < h and 0 <= c < w:
                    if s[r][c] == '#':
                        flg = True
                        break
            if not flg:
                ans_flg = False
                break
        if not ans_flg:
            break
print('Yes' if ans_flg else 'No')

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if grid[i][j] == '#':
            flag = False
            for d in diff:
                if 0 <= i + d[0] < H and 0 <= j + d[1] < W:
                    if grid[i+d[0]][j+d[1]] =='#':
                        flag = True
            if not flag:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys

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

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

# print(A)
for r in range(H):
    for line in range(W):
        # print(r, line)
        if(A[r][line] != "#"):
            continue

        # print(r, line, A[r][line])

        if(line == 0 or W == 1):
            L = False
        else:
            L = A[r][line - 1] == "#"

        if(r == 0 or H == 1):
            U = False
        else:
            U = A[r - 1][line] == "#"

        if(line == W - 1 or W == 1):
            R = False
        else:
            R = A[r][line + 1] == "#"

        if(r == H - 1 or H == 1):
            D = False
        else:
            D = A[r + 1][line] == "#"

        # print(L, U, R, D)
        # print(L or U or R or D)
        if((L or U or R or D) is False):
            print("No")
            sys.exit()

print("Yes")

Submission status: Accepted

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

def countSharp(x,y):
    res = 0
    if x!=0:
        res += s[x-1][y]=='#'
    if y!=0:
        res += s[x][y-1]=='#'
    if x!=h-1:
        res += s[x+1][y]=='#'
    if y!=w-1:
        res += s[x][y+1]=='#'
    return res

flag = [[False for _ in range(w)] for oijqd in range(h)]
for i,l in enumerate(s):
    for j,c in enumerate(l):
        if c=='.':
            flag[i][j] = True

for i in range(h):
    for j in range(w):
        if s[i][j]=='#' and countSharp(i,j)>0:
            flag[i][j] = True
            if i!=0:
                flag[i-1][j] = True
            if j!=0:
                flag[i][j-1] = True
            if i!=h-1:
                flag[i+1][j] = True
            if j!=w-1:
                flag[i][j+1] = True

res = True
for l in flag:
    if False in l:
        res = False
        break

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

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s_list[i][j] == '#':
            flag = False
            for dy in range(-1, 2):
                for dx in range(-1, 2):
                    # 自分自身は無視
                    if dy == 0 and dx == 0:
                        continue
                    # 上下左右に限定(斜めは無視)
                    if dy == 0 or dx == 0:
                        ny = dy + i
                        nx = dx + j
                        # 端っこは無視
                        if ny < 0 or nx < 0 or ny >= h or nx >= w:
                            continue
                        # 1つでも#あれば探索完了
                        if s_list[ny][nx] == '#':
                            flag = True
                            break
                if flag:
                    break
            # 上下左右で#がない#のマスがあればそこで終了
            if not flag:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        ss[i+1][j+1]=s[i][j]

flag = 1

for i in range(h):
    for j in range(w):
        if ss[i+1][j+1]=="#":
            if ss[i][j+1]=="." and ss[i+2][j+1]=="." and ss[i+1][j]=="." and ss[i+1][j+2]==".":
                flag = 0
                break
                
if flag==1:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
m = [list(input()) for _ in range(H)]
for r in range(H):
  for c in range(W-1):
    if m[r][c] != '.' and m[r][c+1] != '.':
      m[r][c],m[r][c+1] = '*','*'
for c in range(W):
  for r in range(H-1):
    if m[r][c] != '.' and m[r+1][c] != '.':
      m[r][c],m[r+1][c] = '*','*'
c = sum([sum([1 if m[r][c] == r'#' else 0 for c in range(W)]) for r in range(H)])
print('No' if c else 'Yes')

Submission status: Accepted

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

bk = '#'
wt = '.'

for r in range(h):
    for c in range(w):
        if M[r][c] == bk:
            flg = False
            for dx, dy in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
                r2 = r + dy
                c2 = c + dx
                if (0 <= r2 < h) and (0 <= c2 < w):
                    if M[r2][c2] == bk:
                        flg = True
                        break
            if not flg:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [[] for i in range(h+2)]
s[0] = '.'*(w+2)
for i in range(1, h+1):
    s[i] = '.' + input() + '.'
s[h+1] = '.'*(w+2)
s2 = [[False for j in range(w)] for i in range(h)]
judge = True
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]] \
            == ['.', '.', '.', '.']:
                judge = False

        else:
            continue
        if not judge:
            break
    if not judge:
        break
if judge:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
line = input().split(" ")
h = int(line[0])
w = int(line[1])

s = []

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

diff = [
    [0,1]
    ,[0,-1]
    ,[1,0]
    ,[-1,0]
]
for hi in range(h):
    for wi in range(w):
        if s[hi][wi]=="#":
            flag = False
            for d in diff:
                if h>hi+d[0]>=0 and w>wi+d[1]>=0:
                    if s[hi+d[0]][wi+d[1]]=="#":
                        flag = True
            if not flag:
                print("No")
                exit()

print("Yes")

Submission status: Accepted

Solution code:
def main():
  t = input().split(" ")
  H, W = int(t[0]), int(t[1])
  
  s = ["."*(W+2)]
  for i in range(H):
    t = input().strip()
    s.append("".join(['.', t, '.']))
  s.append("."*(W+2))
  flag = True
  for i in range(H):
    for j in range(W):
      if s[i+1][j+1] == '#':
        if s[i][j+1]=='.' and s[i+2][j+1]=='.' and s[i+1][j]=='.' and s[i+1][j+2]=='.':
          flag = False
  if flag:
    print("Yes")
  else:
    print("No")
main()

Submission status: Accepted

Solution code:
#!/usr/bin/python3

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def main():
    H, W = map(int, input().split())
    field = []
    for i in range(H):
        field.append(input())

    for y in range(H):
        for x in range(W):
            if field[y][x] == "#":
                ok = False
                for i in range(4):
                    ny = y + dy4[i]
                    nx = x + dx4[i]

                    if inside(ny, nx, H, W) and field[ny][nx] == "#":
                        ok = True

                if not ok:
                    print(No)
                    return
    print(Yes)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

def f():
  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] != '#':
          return 'No'
  return 'Yes'

print(f())

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
s = [input() for i in range(H)]
flag = True
for i in range(H):
  for j in range(W):
    if s[i][j] == '#' and flag:
      flag = False
      if i > 0:
        if s[i-1][j] == '#':
          flag = True
      if j > 0:
        if s[i][j-1] == '#':
          flag = True
      if i < H-1:
        if s[i+1][j] == '#':
          flag = True
      if j < W-1:
        if s[i][j+1] == '#':
          flag = True
if flag:
  print('Yes')
else:
  print('No')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Aug  7 23:17:21 2018

@author: Yuki
"""


def check_good(S, w, h, W, H):
    upper_flag = True
    down_flag = True
    left_flag = True
    right_flag = True
    if w == 0:
        left_flag = False
    if w == W - 1:
        right_flag = False
    if h == 0:
        upper_flag = False
    if h == H - 1:
        down_flag = False
    tmp = []
    if left_flag:
        tmp.append(S[h][w-1] == '#')
    if right_flag:
        tmp.append(S[h][w+1] == '#')
    if upper_flag:
        tmp.append(S[h-1][w] == '#')
    if down_flag:
        tmp.append(S[h+1][w] == '#')
    return sum(tmp)
def main():
    H, W = map(int, input().split())
    S = []
    for i in range(H):
        S.append(input())
    
    Yes_flag = True
    for w in range(W):
        for h in range(H):
            if S[h][w] == '#':
                res = check_good(S, w, h, W, H)
                if res == 0:
                    Yes_flag = False
#                    print(w, h)
            if Yes_flag == False:
                break
        if Yes_flag == False:
            break
    if Yes_flag:
        print('Yes')
    else:
        print('No')

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

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

grid = np.zeros((H, W), dtype=int)

for h in range(H):
    S = input()
    for i, s in enumerate(S):
        if s == '#':
            grid[h][i] = 1

# print(grid)


def is_safe(h, w):
    ns = []
    ns.append((h-1, w))
    ns.append((h, w-1))
    ns.append((h+1, w))
    ns.append((h, w+1))

    safe = False
    for n in ns:
        if 0 <= n[0] and n[0] < H \
           and 0 <= n[1] and n[1] < W \
           and grid[n[0]][n[1]] == 1:
            safe = True

    if safe is False:
        print("No")
        exit(0)

    
for h in range(H):
    for w in range(W):
        if grid[h][w] == 1:
            is_safe(h, w)

print("Yes")
exit(0)

Submission status: Accepted

Solution code:
r, c = (int(x) for x in raw_input().split())
g = [raw_input() for _ in xrange(r)]
pts = [(i, j) for i in xrange(r) for j in xrange(c) if g[i][j] == '#']
good_pts = [(i,j) for (i,j) in pts if (i+1,j) in pts or (i-1,j) in pts or (i,j-1) in pts or (i,j+1) in pts]
print "Yes" if len(pts) == len(good_pts) else "No"

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)

# Debug output
def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

# Binary converter
def to_bin(x):
    return bin(x)[2:]

# --------------------------------------------

dp = None

def main():
    H, W = map(int, input().split())
    S = []
    Dx, Dy = [-1, 0, 0, 1], [0, -1, 1, 0]

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

    for i in range(H):
        for j in range(W):
            if S[i][j] == "#":
                flg = False
                for dx, dy in zip(Dx, Dy):
                    x = i + dx
                    y = j + dy

                    if not (0 <= x < H and 0 <= y < W):
                        continue

                    if S[x][y] == "#":
                        flg = True
                        break
                
                if not flg:
                    print("No")
                    sys.exit()
    
    print("Yes")


main()