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=list(map(int,input().split()))

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

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            if i == 0 and j == 0:
                if S[0][1] == "." and S[1][0] == ".":
                    print("No")
                    exit()
            elif i == 0 and j == W-1:
                if S[0][W-2] == "." and S[1][W-1] == ".":
                    print("No")
                    exit()
            elif i == H-1 and j == 0:
                if S[H-1][1] == "." and S[H-2][0] == ".":
                    print("No")
                    exit()
            elif i == H-1 and j == W-1:
                if S[H-1][W-2] == "." and S[H-2][W-1] == ".":
                    print("No")
                    exit()
            elif i == 0 and j >= 1:
                if S[i][j-1] == "." and S[i][j+1] == "." and S[i+1][j] == ".":
                    print("No")
                    exit()
            elif i == H-1 and j >=1:
                if S[i][j-1] == "." and S[i][j+1] == "." and S[i-1][j] == ".":
                    print("No")
                    exit()
            elif i >= 1 and j == 0:
                if S[i][j+1] == "." and S[i-1][j] == "." and S[i+1][j] == ".":
                    print("No")
                    exit()
            elif i >= 1 and j == W-1:
                if S[i][j-1] == "." and S[i-1][j] == "." and S[i+1][j] == ".":
                    print("No")
                    exit()
            elif i >=1 and j >= 1:
                if S[i][j-1] == "." and S[i][j+1] == "." and S[i-1][j] == "." and S[i+1][j] == ".":
                    print("No")
                    exit()
print("Yes")

Submission status: Accepted

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

for i in range(H):
  for j in range(W):
    if sw[i][j] == "#":
      counter = 0
      for k in range(4):
        if i+dx[k] < 0 or i+dx[k] >= H or j+dy[k] < 0 or j+dy[k] >= W:
          counter += 1
        elif sw[i+dx[k]][j+dy[k]] == ".":
          counter += 1
      if counter == 4:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

Solution code:
def calc(x, y):
    x2 = x
    y2 = y - 1
    x4 = x - 1
    y4 = y
    x5 = x + 1
    y5 = y
    x7 = x
    y7 = y + 1
    return x2, y2, x4, y4, x5, y5, x7, y7

h, w = map(int, input().split())
s = []
for _ in range(h):
    s.append(list(input()))
ans = "Yes"
for i in range(h * w):
    x = i // h
    y = i % h
    c = 0
    if s[y][x] == "#":
        x2, y2, x4, y4, x5, y5, x7, y7 = calc(x, y)
        aaa = [[x2, y2], [x4, y4], [x5, y5], [x7, y7]]
        for (xx, yy) in aaa:
            if ((xx >= 0) & (xx < w) & (yy >= 0) & (yy < h)):
                if s[yy][xx] == "#":
                    c += 1
        if c == 0:
            ans = "No"
            break
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000)

H,W = [int(x) for x in input().split()]
S = [list('.'*(W+2))]+[list('.'+input()+'.') for _ in range(H)]+[list('.'*(W+2))]
lix = [-1,0,1,0]
liy = [0,-1,0,1]
flag = True
def dfs(x,y):
    cnt = 0
    if(S[x][y]=='.'):
        return 0
    else:
        cnt += 1
        S[x][y]='.'
        for i in range(4):
            if(S[x+lix[i]][y+liy[i]]!='.'):
                cnt += dfs(x+lix[i],y+liy[i])
        return cnt

for i in range(1,H+1):
    for j in range(1,W+1):
          if(dfs(i,j)==1):
              flag = False
if(flag):
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

	for i in range(H):
		for j in range(W):
			if S[i][j] == ".":
				continue
			if (
				(i > 0 and S[i-1][j] == "#")
				or (j > 0 and S[i][j-1] == "#")
				or (i < H-1 and S[i+1][j] == "#")
				or (j < W-1 and S[i][j+1] == "#")
				):
				continue
			print("No")
			return

	print("Yes")


if __name__ == '__main__':
    main()

Submission status: Accepted

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


pics = []
tmp = ["."] * (w+2)
pics.append(tmp)
for i in range(h):
  tmp = list(input())
  tmp.insert(0,".")
  tmp.append(".")
  pics.append(tmp)
  
tmp = ["."] * (w+2) 
pics.append(tmp)
ans = "Yes"  

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=[]
for i in range(h):
    li=[]
    for j in input():
        if j==".":li.append(0)
        elif j=="#":li.append(1)
    l.append(li)
for i in range(h):
    for j in range(w):
        if l[i][j]==1:
            x=0
            if i>0:x+=l[i-1][j]
            if i<h-1:x+=l[i+1][j]
            if j>0:x+=l[i][j-1]
            if j<w-1:x+=l[i][j+1]
            if x==0:
                print("No")
                break
    else:continue
    break
else:print("Yes")

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
import collections
from operator import itemgetter
#以下てんぷら
def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)

#n = ip()                                     #入力整数1つ
h,w= (int(i) for i in input().split())       #入力整数横2つ以上
#a = [int(i) for i in input().split()]        #入力整数配列
## a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#k = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)
a=[input() for i in range(h)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
#こっから本文
for i in range(h):
    for k in range(w):
        if a[i][k] == '#':
            fail = 1
            if i:
                if a[i - 1][k] == '#': fail = 0
            if i != h-1:
                if a[i + 1][k] == '#': fail = 0

            if k:
                if a[i][k - 1] == '#': fail = 0
            if k != w-1:
                if a[i][k + 1] == '#': fail = 0
            if fail:j(0)
j(1)

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
map = []
for i in range(H):
    tmp = list(str(input()))
    tmp.insert(0,".")
    tmp.append(".")
    map.append(tmp)
tmp = ["."]*(W+2)
map.insert(0,tmp)
map.append(tmp)


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

Submission status: Accepted

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

HW = []
for h in range(H):
    w = list(input())
    HW.append(w)

flg = True


def check(h, w):
    if h == 0:
        if w == 0:
            if HW[h + 1][w] == "#" or HW[h][w + 1] == "#":
                return True
        elif w == W - 1:
            if HW[h + 1][w] == "#" or HW[h][w - 1] == "#":
                return True
        else:
            if HW[h + 1][w] == "#" or HW[h][w + 1] == "#" or HW[h][w-1] == "#":
                return True
    elif h == H - 1:
        if w == 0:
            if HW[h - 1][w] == "#" or HW[h][w + 1] == "#":
                return True
        elif w == W - 1:
            if HW[h - 1][w] == "#" or HW[h][w - 1] == "#":
                return True
        else:
            if HW[h - 1][w] == "#" or HW[h][w + 1] == "#" or HW[h][w-1] == "#":
                return True
    else:
        if w == 0:
            if HW[h - 1][w] == "#" or HW[h + 1][w] == "#" or HW[h][w+1] == "#":
                return True
        elif w == W-1:
            if HW[h - 1][w] == "#" or HW[h + 1][w] == "#" or HW[h][w-1] == "#":
                return True
        else:
            if HW[h - 1][w] == "#" or HW[h + 1][w] == "#" or \
                    HW[h][w-1] == "#" or HW[h][w+1] == "#":
                return True
    return False


for i in range(0, H):

    if flg is False:
        break
    for j in range(0, W):
        if HW[i][j] == "#":
            flg = check(i, j)
            # print(i, j, check(i, j))
        if flg is False:
            break

if flg is True:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

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

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]

def i_inpl(): return int(input())
def s_inpl(): return map(int,input().split())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

#############################################

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

flag = True

for y in range(H):
    for x in range(W):
        if f[y][x] == ".":
            continue
        tmp = False
        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if inside(ny, nx, H, W) and f[ny][nx] == "#":
                tmp = True
                break
        if not tmp:
            flag = False
            break

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(h):
  for j in range(w):
    if j > 0 and a[i][j-1] != "." and a[i][j] == "#":
      a[i][j] = "*"
    elif j < w - 1 and a[i][j+1] != "." and a[i][j] == "#":
      a[i][j] = "*"
    elif i > 0 and a[i-1][j] != "." and a[i][j] == "#":
      a[i][j] = "*"
    elif i < h - 1 and a[i+1][j] != "." and a[i][j] == "#":
      a[i][j] = "*"

print("No" if sum([row.count("#") for row in a]) else "Yes")

Submission status: Accepted

Solution code:
# coding: utf-8
H, W = map(int, input().split())

campus = []

for i in range(H):
    campus.append(list(input()))


def search_lonely_sharp(l :list) -> list:
    _lonely_sharps = []
    ls = ['.'] + l + ['.']
    for i in range(1, len(ls)-1):
        if ls[i] == '#' and ls[i] != ls[i-1] and ls[i] != ls[i+1]:
            _lonely_sharps.append(i-1)
    return _lonely_sharps


for i in range(1, H-1):
    before_line = campus[i-1]
    line = campus[i]
    after_line = campus[i+1]
    lonely_sharps = search_lonely_sharp(line)
    for j in lonely_sharps:
        if before_line[j] != '#' and after_line[j] != '#':
            print('No')
            exit()

print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [['.'] * (W + 2)] + [list('.' + input() + '.') for _ in range(H)] + [['.'] * (W + 2)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if s[h][w] == '.':
            continue
        if s[h - 1][w] == s[h + 1][w] == s[h][w - 1] == s[h][w + 1] == '.':
            print('No')
            exit()
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [['.'] * (W + 2)] + [list('.' + input() + '.') for _ in range(H)] + [['.'] * (W + 2)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if s[h][w] == '.':
            continue
        bcell = 0
        for x, y in ((-1, 0), (1, 0), (0, -1), (0, 1)):
            if s[h + x][w + y] == '#':
                bcell += 1
        if bcell < 1:
            print('No')
            exit()
print('Yes')

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        if data[h][w] == '#':
            if h - 1 >= 0:
                if data[h-1][w] == '#':
                    continue
            if w - 1 >= 0:
                if data[h][w-1] == '#':
                    continue
            if h + 1 <= H - 1:
                if data[h+1][w] == '#':
                    continue
            if w + 1 <= W - 1:
                if data[h][w+1] == '#':
                    continue
            print('No')
            exit()
print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=["."*(w+2)]
for i in range(h):
  l+=["."+input()+"."]
l+=["."*(w+2)]
ans=0
for i in range(1,h+1):
  for j in range(1,w+1):
    if l[i][j]==".":
      continue
    if l[i+1][j]=="#" or l[i-1][j]=="#" or l[i][j+1]=="#" or l[i][j-1]=="#":
      continue
    ans=1
print("YNeos"[ans::2])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def yes():
    print("Yes")  # type: str


def no():
    print("No")  # type: str


def solve(H: int, W: int, s: "List[str]"):
    n = [[0]*(W+2) for _ in range(H+2)]
    for i in range(H):
        for j in range(W):
            if s[i][j] == "#":
                n[i+1][j+1] = 1  # 外枠を作りつつ

    flag = True
    for i in range(1, H+1):
        for j in range(1, W+1):
            # print(i, j)
            if n[i][j] == 1 and n[i-1][j]+n[i+1][j]+n[i][j-1]+n[i][j+1] == 0:
                flag = False
                break
        if flag == False:
            break
    if flag == False:
        no()
    else:
        yes()

    return


def main():

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

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

for i in range(H):
    for j in range(W):
        flg = 0
        if s[i][j] == '#':
            for k in range(4):
                x = i + [1, 0, -1, 0][k]
                y = j + [0, 1, 0, -1][k]
                if x < 0 or x >= H or y < 0 or y >= W:
                    continue
                if s[x][y] == '#':
                    flg = 1
            if flg == 0:
                print('No')
                sys.exit()

print('Yes')

Submission status: Accepted

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

ans="Yes"
for h in range(H):
	for w in range(W):
		if m[h][w] == "#":
			ok = False
			if h-1 >= 0 and m[h-1][w] == "#":
				ok = True
			if w-1 >= 0 and m[h][w-1] == "#":
				ok = True
			if h+1 < H and m[h+1][w] == "#":
				ok = True
			if w+1 < W and m[h][w+1] == "#":
				ok = True
			if not ok:
				ans="No"
print(ans)

Submission status: Accepted

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


picture = []
diff = [-1, 1]

for i_h in range(h):
    row = ''
    for i_w in range(w):
        can_draw = False
        if S[i_h][i_w] == '.': row = row + '.';continue
        else:
            for dh in diff:
                if (i_h+dh<0) or (i_h+dh>h-1):continue
                if S[i_h+dh][i_w] == '#':
                    can_draw = True
            for dw in diff:
                if (i_w+dw<0) or (i_w+dw>w-1):continue
                if S[i_h][i_w+dw] == '#':
                    can_draw = True
                    
        if can_draw:
            row = row + '#'
        else:
            row = row + '.'
    picture.append(row)
    
if picture == S:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
import sys
from sys import stdin
H, W = [int(_) for _ in stdin.readline().rstrip().split()]
data = [list(stdin.readline().rstrip()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        if data[i][j] == '#':
            if i==0:
                if j==0:
                    if data[i][j+1] == '.' and data[i+1][j] == '.':
                        print('No')
                        sys.exit()
                elif j==W-1:
                    if data[i][j-1] == '.' and data[i+1][j] == '.':
                        print('No')
                        sys.exit()
                else:
                    if data[i][j+1] == '.' and data[i][j-1] == '.' and data[i+1][j] == '.':
                        print('No')
                        sys.exit()
            elif i==H-1:
                if j==0:
                    if data[i][j+1] == '.' and data[i-1][j] == '.':
                        print('No')
                        sys.exit()
                elif j==W-1:
                    if data[i][j-1] == '.' and data[i-1][j] == '.':
                        print('No')
                        sys.exit()
                else:
                    if data[i][j+1] == '.' and data[i][j-1] == '.' and data[i-1][j] == '.':
                        print('No')
                        sys.exit()
            else:
                if j==0:
                    if data[i][j+1] == '.' and data[i+1][j] == '.' and data[i-1][j] == '.':
                        print('No')
                        sys.exit()
                elif j==W-1:
                    if data[i+1][j] == '.' and data[i][j-1] == '.' and data[i-1][j] == '.':
                        print('No')
                        sys.exit()
                else:
                    if data[i][j+1] == '.' and data[i][j-1] == '.' and data[i-1][j] == '.' and data[i+1][j] == '.':
                        print('No')
                        sys.exit()
print('Yes')

Submission status: Accepted

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

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

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
            print('No')
            quit()
print('Yes')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2019/04/16
# AtCoder Beginner Contest 096 - C

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

ans = "Yes"

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

print(ans)

Submission status: Accepted

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

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

def f(sss, i, j):
    if sss[i][j] == '.':
        return True
    for v in (-1, 1):
        if i+v < 0 or h <= i+v:
            continue
        if sss[i+v][j] == '#':
            return True
    for u in (-1, 1):
        if j+u < 0 or w <= j+u:
            continue
        if sss[i][j+u] == '#':
            return True
    return False

def solve(sss):
    for i in range(h):
        for j in range(w):
            if not f(sss, i, j):
                return False
    return True

print("Yes" if solve(sss) else "No")

Submission status: Accepted

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

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

print("Yes")

Submission status: Accepted

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

for i in range(h):
  for j in range(w):
    if s[i][j] == "#":
      top = max(0, i-1)
      bottom = min(h-1, i+1)
      right = min(w-1, j+1)
      left = max(0, j-1)
      if s[i][right] == s[i][left] == s[top][j] == s[bottom][j] == ".":
        print("No")
        exit()

print("Yes")

Submission status: Accepted

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

for i in range(h):
  for j in range(w):
    if s[i][j] == "#":
      top = max(0, i-1)
      bottom = min(h-1, i+1)
      right = min(w-1, j+1)
      left = max(0, j-1)
      if s[i][right] != "#" and s[i][left] != "#" and s[top][j] != "#" and s[bottom][j] != "#":
        print("No")
        exit()

print("Yes")

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] == "#":
      top = max(0, i-1)
      bottom = min(h-1, i+1)
      right = min(w-1, j+1)
      left = max(0, j-1)
      if s[i][right] != "#" and s[i][left] != "#" and s[top][j] != "#" and s[bottom][j] != "#":
        flag = False
        break
  if flag == False:
    break

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

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


def inRange(h: int, w: int, H: int, W: int):
    return h >= 0 and w >= 0 and h < H and w < W


def solve(H: int, W: int, s: "List[str]"):
    failed = False
    for h in range(H):
        for w in range(W):
            cur = s[h][w]
            if cur == "#":
                if inRange(h-1, w, H, W) and s[h-1][w] == "#":
                    continue
                if inRange(h+1, w, H, W) and s[h+1][w] == "#":
                    continue
                if inRange(h, w-1, H, W) and s[h][w-1] == "#":
                    continue
                if inRange(h, w+1, H, W) and s[h][w+1] == "#":
                    continue
                failed = True
                break
        if failed:
            break
    print("No") if failed else print("Yes")
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(H)]  # type: "List[str]"
    solve(H, W, s)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

def chk(i,j):
    if a[i][j]==".": return 0
    if i>0 and a[i-1][j]=="#" :return 0
    if i<H-1 and a[i+1][j]=="#": return 0
    if j>0 and a[i][j-1]=="#" :return 0
    if j<W-1 and a[i][j+1]=="#": return 0        
    return 1

H,W=map(int,input().split())
a=[input() for i in range(H)]

for i in range(H):
    for j in range(W):
        if chk(i,j): print("No"),sys.exit()

print("Yes")

Submission status: Accepted

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

smap = [''.join('.' for _ in range(w+2))]
for _ in range(h):
  smap.append('.' + input().strip() + '.')
smap.append(''.join('.' for _ in range(w+2)))

is_black = True
for i, sline in enumerate(smap):
  for j, s in enumerate(sline):
    if s == '#':
      if smap[i][j-1] == '.' and smap[i][j+1] == '.' and smap[i-1][j] == '.' and smap[i+1][j] == '.':
        is_black = False

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

Submission status: Accepted

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

around = [[-1,0],[0,-1],[0,1],[1,0]]
clear = True
for i in range(1,H+1):
    for j in range(1,W+1):
        if s[i][j] == '#':
            if all((s[i+k[0]][j+k[1]] == '.' for k in around)):
                clear = False
                break
    else:
        continue
    break

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

Submission status: Accepted

Solution code:
def main():
    input = sys.stdin.readline
    try:
        H,W = map(int,input().split())
        s = [list(input()) for _ in range(H)]
        dir4 = ((0, 1), (0, -1), (1, 0), (-1, 0))
        for i in range(H):
            for j in range(W):
                if s[i][j] == ".": continue
                cnt = 0
                for d in dir4:
                    nx = i + d[0]
                    ny = j + d[1]
                    if not (0 <= nx < H): continue
                    if not (0 <= ny < W): continue
                    if s[nx][ny] == "#": cnt += 1
                if cnt == 0:
                    print("No")
                    exit()
        print("Yes")
        
    finally:
        input = None

import sys; sys.setrecursionlimit(50000)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
H, W = [int(i) for i in input().split()]
list = [list(input()) for i in range(H)]
dxy = [(-1,0), (1, 0), (0, 1), (0, -1)]
flg = True
for i in range(H):
    for j in range(W):
        count = 0
        if list[i][j] == ".":
            continue
        if list[i][j] == "#":
            for l, m in dxy:
                if (i + l < 0) or (i + l > H -1) or (j + m < 0) or (j + m > W -1):
                    continue
                if list[i + l][j + m] == "#":
                    count += 1
        if count == 0:
            flg = False
if flg:
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

out = "Yes"
for i in range(1,H+1):
    for j in range(1,W+1):
        if s[i][j] == "." :
            continue
        for k,l in ((1,0),(-1,0),(0,1),(0,-1)):
            if s[i+k][j+l] == "#":
                break
        else:
            out = "No"
            break
    if out=="No":
        break
print(out)

Submission status: Accepted

Solution code:
def count(i,j):
  count = 0
  if list[i][j-1]=='#':
    count +=1
  if list[i][j+1]=='#':
    count +=1
  if list[i-1][j]=='#':
    count +=1
  if list[i+1][j]=='#':
    count +=1
  if count >= 1:
    return True
  else:
    return False

h,w = map(int, input().split())
s_init='0'*(w+2)
flag=True
list=[]
list.append(s_init)
for i in range(h):
  s=str(input())
  list.append('0'+s+'0')
list.append(s_init)
for i in range(1,h+1):
  for j in range(1,w+1):
    if list[i][j]=='#':
      if not(count(i,j)):
        flag=False
if flag:
  print('Yes')
else:
  print('No')