Problem:


Score: 300 points

Problem Statement

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

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

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

Determine if square1001 can achieve his objective.  

Constraints

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

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

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

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

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

Sample Input 2

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

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

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

Sample Output 3

Yes



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


Sample output:
Yes

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=list(input()for _ in range(h))
if h==w==1:
    print('No')
    exit()
elif h==1:
        for i in range(w):
            if s[0][i]=='#':
                if i==0:
                    if s[0][i+1]=='.':
                        print('No')
                        exit()
                elif i==w-1:
                    if s[0][i-1]=='.':
                        print('No')
                        exit()
                else:
                    if s[0][i-1]=='.'and s[0][i+1]=='.':
                        print('No')
                        exit()
elif w==1:
        for i in range(h):
            if s[i][0]=='#':
                if i==0:
                    if s[i+1][0]=='.':
                        print('No')
                        exit()
                elif i==w-1:
                    if s[i-1][0]=='.':
                        print('No')
                        exit()
                else:
                    if s[i-1][0]=='.'and s[i+1][i-1]=='.':
                        print('No')
                        exit()
else:
    for i in range(h):
      for j in range(w):
        if s[i][j]=='#':
            if i==0:
                if j==0:
                    if s[i+1][j]=='.'and s[i][j+1]=='.':
                        print('No')
                        exit()
                if j==w-1:
                    if s[i+1][j]=='.'and s[i][j-1]=='.':
                        print('No')
                        exit()
                else:
                    if s[i+1][j]=='.'and s[i][j+1]=='.' and s[i][j-1]=='.':
                        print('No')
                        exit()
            if i==h-1:
                if j==0:
                    if s[i-1][j]=='.'and s[i][j+1]=='.':
                        print('No')
                        exit()
                if j==w-1:
                    if s[i-1][j]=='.'and s[i][j-1]=='.':
                        print('No')
                        exit()
                else:
                    if s[i-1][j]=='.'and s[i][j+1]=='.' and s[i][j-1]=='.':
                        print('No')
                        exit()
            else:
                if j==0:
                    if s[i-1][j]=='.' and s[i+1][j]=='.' and s[i][j+1]=='.':
                        print('No')
                        exit()
                if j==w-1:
                    if s[i-1][j]=='.' and s[i+1][j]=='.' and s[i][j-1]=='.':
                        print('No')
                        exit()
                else:
                    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())

temp = ['.'] * (W + 2)
S = [temp]

for _ in range(H):
    S.append(list('.' + input() + '.'))

S.append(temp)

output = []

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

print('Yes')

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

H,W = IL()
S = SLs(H)
dx = [-1,0,1,0,]
dy = [0,-1,0,1,]

for x in range(H):
  for y in range(W):
    if S[x][y] == "#":
      flag = 0
      for i in range(4):
        nx = dx[i]+x
        ny = dy[i]+y
        if 0<=nx<H and 0<=ny<W:
          if S[nx][ny] == "#":
            flag = 1
      if flag == 0:
        NE()
Y()

Submission status: Accepted

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

iso_flag = [[True] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s_list[i][j] == ".":
            continue

        flag_r = True
        if j < w - 1 and s_list[i][j + 1] == "#":
            flag_r = False
            iso_flag[i][j + 1] = False
        
        flag_d = True
        if i < h - 1 and s_list[i + 1][j] == "#":
            flag_d = False
            iso_flag[i + 1][j] = False
        
        if iso_flag[i][j] and flag_r and flag_d:
            print("No")
            exit()
print("Yes")

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j]=='#':
            cnt=0
            for k,l in [(1,0),(0,1),(-1,0),(0,-1)]:
                ny,nx=i+k,j+l
                if nx>=0 and nx<w and ny>=0 and ny<h:
                    if s[ny][nx]=='#':
                        cnt+=1
            if cnt==0:
                print('No')
                sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
a = [["."] + list(input()) + ["."] for i in range(h)]
a = [["."] * (w + 2)] + a + [["."] * (w + 2)]
b = []
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if a[i][j - 1] == "." and a[i][j] == "#" and a[i][j + 1] == ".":
            b.append([i, j])
for i in b:
    x, y = i
    if a[x - 1][y] == "." and a[x + 1][y] == ".":
        print("No")
        break
else:
    print("Yes")

Submission status: Accepted

Solution code:
def resolve():
    # 整数 1 つ
    # n = int(input())
    # 整数複数個
    h, w = map(int, input().split())
    # 整数 N 個 (改行区切り)
    S = [list(input()) for i in range(h)]
    # 整数 N 個 (スペース区切り)
    # N = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    flag = True
    for i in range(h):
        for j in range(w):
            if S[i][j] == ".":
                continue
            else:
                neighbor = False
                N = [[i-1, j], [i+1, j], [i, j-1], [i, j+1]]
                for s in N:
                    if s[0] >= 0 and s[0] <= h-1 and s[1] >= 0 and s[1] <= w-1:
                        if S[s[0]][s[1]] == "#":
                            neighbor = True
                            break
#                print(neighbor)
                if neighbor == False:
                    flag = False
                    break
        if flag == False:
            break

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


resolve()

Submission status: Accepted

Solution code:
h, w=map(int, input().split())
s=['.'*(w+2)]+['.'+input()+'.' for _ in range(h)]+['.'*(w+2)]
ans='Yes'
for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j]=='#':
      if '#' not in {s[i-1][j], s[i+1][j], s[i][j-1], s[i][j+1]}:
        ans='No'
        break
        break
print(ans)

Submission status: Accepted

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

def search(i, j):
  pts = set([ (max(0, i-1), j), (i, max(0, j-1)), 
        (min(h-1, i+1), j), (i, min(w-1, j+1))])
  
  for dh, dw in list(pts):
    if maps[dh][dw] == "#":
      return True
  return False

flag = True
for i in range(h):
  for j in range(w):
    if maps[i][j] == "#": 
      if not search(i, j):
        flag = False
        break
        
print("Yes" if flag else "No")

Submission status: Accepted

Solution code:
def main():
    H, W = list(map(int, input().split(' ')))
    S = [input() for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if S[h][w] == '.':
                continue
            # check adjacent cells
            if h - 1 >= 0 and S[h - 1][w] == '#':
                continue
            if h + 1 <= H - 1 and S[h + 1][w] == '#':
                continue
            if w - 1 >= 0 and S[h][w - 1] == '#':
                continue
            if w + 1 <= W - 1 and S[h][w + 1] == '#':
                continue
            # all adjacent cells are white -> NG
            print('No')
            return
    print('Yes')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

h, w = map(int, input().split())
s = []

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

for y, r in enumerate(s):
    for x, c in enumerate(r):
        if c == '#':
            check_str = ''
            
            if x > 0:
                check_str+=r[x-1]
            if x < len(r) - 1:
                check_str+=r[x+1]
            if y > 0:
                check_str+=s[y-1][x]
            if y < len(s) - 1:
                check_str+=s[y+1][x]
            
            if not '#' in check_str:
                print('No')
                exit()

print('Yes')

Submission status: Accepted

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

la = ["."] * (w+2)
lz = ["."] * (w+2)

l = list()
l.append(la)

for i in range(h):
    l.append(["."] + list(input()) + ["."])

l.append(lz)

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

print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int, input().split(" "))
s = []
for row in range(h):
    line = input()
    tmp = []
    for column in range(w):
        tmp.append(line[column])
    s.append(tmp)

sx = [-1, 0, 0, 1]
sy = [0, 1, -1, 0]

res = True
for row in range(h):
    for column in range(w):
        if s[row][column] == "#":
            count = 0
            for i in range(len(sx)):
                row_target = row + sx[i]
                column_target = column + sy[i]
                if row_target < 0 or len(s) <= row_target:
                    continue
                if column_target < 0 or len(s[row]) <= column_target:
                    continue
                if s[row_target][column_target] == "#":
                    count += 1
            if count == 0:
                res = False

print("Yes" if res == True else "No")

Submission status: Accepted

Solution code:
h, w = map(int, input().split(" "))
s = []
for row in range(h):
    line = input()
    tmp = []
    for column in range(w):
        tmp.append(line[column])
    s.append(tmp)

sx = [-1, 0, 0, 1]
sy = [0, 1, -1, 0]

res = True
for row in range(h):
    for column in range(w):
        if s[row][column] == "#":
            count = 0
            for i in range(len(sx)):
                if row + sx[i] < 0 or len(s) <= row + sx[i]:
                    continue
                if column + sy[i] < 0 or len(s[row]) <= column + sy[i]:
                    continue
                if s[row + sx[i]][column + sy[i]] == "#":
                    count += 1
            if count == 0:
                res = False

print("Yes" if res == True else "No")

Submission status: Accepted

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

ans_list = []
for h in range(1, H+1):
    for w in range(1, W+1):
        if s_list[h][w]=='#':
            if s_list[h-1][w]=='#':
                ans = True
            elif s_list[h+1][w]=='#':
                ans = True
            elif s_list[h][w-1]=='#':
                ans = True
            elif s_list[h][w+1]=='#':
                ans = True
            else:
                ans = False
            ans_list.append(ans)

if len(ans_list)==sum(ans_list):
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

campus = []

campus.append("."*(W+2))
for _ in range(H):
    s = list("."+input()+".")
    campus.append(s)
campus.append("."*(W+2))

# print(campus)

flag = True
for i in range(1, H+1):
    for j in range(1, W+1):
        if campus[i][j] == "#":
            if campus[i-1][j] != "#" and campus[i+1][j] != "#" and campus[i][j-1] != "#" and campus[i][j+1] != "#":
                flag = False

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

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s_list = [list(input()) for nesya in range(h)]
for s in s_list:
  s.insert(0,'.')
  s.append('.')
a = ['.']*(w+2)
s_list.insert(0,a)
s_list.append(a)
c = 0
for i in range(1,w+1):
  for j in range(1,h+1):
    if c == 1:
      break
    if s_list[j][i] == '#':
      if s_list[j-1][i] == s_list[j+1][i] == s_list[j][i-1] == s_list[j][i+1] == '.':
        c += 1
        print('No')
        break
if c == 0:
  print('Yes')

Submission status: Accepted

Solution code:
from itertools import product
H, W = list(map(int, input().split()))
board = [['.'] * W] + [list(input()) for _ in range(H)] + [['.'] * W]
board = [['.'] + b + ['.'] for b in board]
dhw = [[1, 0], [-1, 0], [0, 1], [0, -1]]
for h, w in product(range(1, H + 1), range(1, W + 1)):
    if board[h][w] == '#':
        can = False
        for dh, dw in dhw:
            if board[h + dh][w + dw] == '#':
                can = True
                break
        if not can:
            print('No')
            exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys
H, W = map(int, input().split())
s = [input() for i in range(H)]
S = []
S.append('.' * (W + 2))
for i in range(H):
    S.append('.' + s[i] + '.')
S.append('.' * (W + 2))
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] == '#':
            if S[i + 1][j] == '#' 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:
#import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
pri = lambda x: print(*x, sep='\n')

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

def check(ny, nx):
    if 0<=ny<H and 0<=nx<W and S[ny][nx]=='#':
        return True
    else:
        return False

ddy = [-1, 0, 1, 0]
ddx = [0, -1, 0, 1]

def dfs(y, x, S):
    deq = deque()
    deq.append([(y, x)]) # insert indices
    S[y][x] = '.'
    found = False
    while deq[0]:
        nqueue = []
        cqueue = deq.pop()
        for val in cqueue:
            y, x = val
            for dy, dx in zip(ddy, ddx):
                ny, nx = y+dy, x+dx
                if check(ny, nx):
                    found = True
                    S[ny][nx] = '.'
                    nqueue.append((ny, nx))
        if found == False:
            print('No')
            exit()
        deq.append(nqueue)

for y in range(H):
    for x in range(W):
        if S[y][x] == '#':
            dfs(y, x, S)
else:
    print('Yes')

Submission status: Accepted

Solution code:
H,W = list(map(int,input().split()))
S = [list(input()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            if not (i == 0 or S[i-1][j] == '.'):
                continue
            if not (i == H-1 or S[i+1][j] == '.'):
                continue
            if not (j == 0 or S[i][j-1] == '.'):
                continue
            if not (j == W-1 or S[i][j+1] == '.'):
                continue
            print('No')
            exit()
print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[]
for _ in range(h):
    li=list(input())
    s.append(li)
#print(s)
#print(s[h-1][w-1])

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

if flg==0:
    print("Yes")

Submission status: Accepted

Solution code:
import sys

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

# 外枠を「.」で囲う準備
li = []
for i in range(H+2):
    li.append(["."]*(W+2))

# 外枠付きSの生成
for i in range(1, H+1):
    li[i][1:-1] = S[i-1]

# 上下左右に「#」が隣接していれば必ず塗れる
for h in range(1, H+1):
    for w in range(1, W+1):
        tmp = 0
        if li[h][w]=='#':
            if li[h - 1][w]=="#":
                tmp += 1
            if li[h + 1][w]=="#":
                tmp += 1
            if li[h][w - 1]=="#":
                tmp += 1
            if li[h][w + 1]=="#":
                tmp += 1
            if tmp==0:
                print("No")
                sys.exit()

print("Yes")

Submission status: Accepted

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

data = [ [0 for _ in range(w)] for _ in range(h) ]
data2 = [ [0 for _ in range(w+2)] for _ in range(h+2) ]

for i in range(h):
  for j in range(w):
    if s[i][j] == '#':
      data[i][j] = 1

for i in range(h+2):
  if i == 0 or i == h+1:
    continue 
  for j in range(w+2):
    if j == 0 or j == w+1:
      continue
    data2[i][j] = data[i-1][j-1]

ck = 1
for i in range(1,h+1):
  for j in range(1,w+1):
    if data2[i][j] :
      if not (data2[i-1][j] or data2[i][j+1] or data2[i+1][j] or data2[i][j-1]) :
        ck = 0
        break
  if not ck :
    break

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

Submission status: Accepted

Solution code:
dy = [1, -1, 0, 0]
dx = [0, 0, 1, -1]

def resolve():
	h, w = map(int, input().split())
	pic = []
	for i in range(h):
		r = list(input())
		pic.append(r)
	for i in range(h):
		for j in range(w):
			if pic[i][j] == '#':
				ok = False
				for k in range(4):
					ny = i + dy[k]
					nx = j + dx[k]
					if ny >= h or nx >= w:
						continue
					if pic[ny][nx] == '#':
						ok = True
				if not ok:
					print('No')
					return
	print('Yes')
resolve()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [input() for _ in range(h)]
dx = [-1, 0, 0, 1]
dy = [0, -1, 1, 0]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            flag = 0
            for k in range(4):
                ny = i + dy[k]
                nx = j + dx[k]
                if nx < 0 or w <= nx:
                    continue
                if ny < 0 or h <= ny:
                    continue
                if s[ny][nx] == '#':
                    flag = 1
            if flag == 0:
                print('No')
                exit()
else:
    print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [['#']*(w+2) for _ in [0]*(h+2)]
for i in range(h):
    s[i+1][1:-1] = input()
for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j]=='#':
            if s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i][j+1]=='....':
                print('No')
                break
    else: continue
    break
else:
    print('Yes')

Submission status: Accepted

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

s = []
check = 0

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

for i in range(h):
    for j in range(w):
        up = 0
        left = 0
        right = 0
        down = 0

        if s[i][j] == ".":
            pass
        else:
            if i == 0:
                up = 0
                if s[i+1][j] == "#":
                    down = 1
            elif i == h-1:
                down == 0
                if s[i-1][j] == "#":
                    up = 1
            else:
                if s[i-1][j] == "#":
                    up = 1
                if s[i+1][j] == "#":
                    down = 1
            if j == 0:
                left = 0
                if s[i][j+1] == "#":
                    right = 1
            elif j == w-1:
                right = 0
                if s[i][j-1] == "#":
                    left = 1
            else:
                if s[i][j-1] == "#":
                    left = 1
                if s[i][j+1] == "#":
                    right = 1

            if up+left+right+down == 0:
                check = 1

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

Submission status: Accepted

Solution code:
from collections import defaultdict

H, W = map(int, input().split())
pict = defaultdict(lambda: "#")
for i in range(H):
    S = input()
    for j in range(len(S)):
        pict[i, j] = S[j]

ans = "Yes"
for i, j in pict.keys():
    if pict[i, j] == "#" and (
        (i - 1 >= 0 and pict[i - 1, j] != "#")
        and (j - 1 >= 0 and pict[i, j - 1] != "#")
        and (i + 1 < H and pict[i + 1, j] != "#")
        and (j + 1 < W and pict[i, j + 1] != "#")
    ):
        ans = "No"
        break

print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [input().strip() for _ in range(h)]
for i in range(h):
    for j in range(w):
        flg = False
        if s[i][j] == '#':
            if 0 < i and s[i - 1][j] == '#':
                flg = True
            if i < (h - 1) and s[i + 1][j] == '#':
                flg = True
            if 0 < j and s[i][j - 1] == '#':
                flg = True
            if j < (w - 1) and s[i][j + 1] == '#':
                flg = True
            if not flg:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
#ABC096-C
h,w = map(int,input().split())
grid = [input() for _ in range(h)]

def grid_search(x,y,grid):
    now = grid[x][y]
    flg = True
    #上下左右を見る
    if now == '#':
        flg = False
        
        if x+1<=h-1 and grid[x+1][y] == '#':
            flg = True
        elif x-1 >= 0 and grid[x-1][y] == '#':
            flg = True
        elif y+1 <= w-1 and grid[x][y+1] == '#':
            flg = True
        elif y-1 >= 0 and grid[x][y-1] == '#':
            flg = True
        else:
            flg = False
            
        if not flg:
            return False
        
    return True
flg = True    
for i in range(h):
    for j in range(w):
        if not grid_search(i,j,grid) and flg:
            print('No')
            flg = False
            break
if flg:
    print('Yes')

Submission status: Accepted

Solution code:
import itertools,sys
def S(): return sys.stdin.readline().rstrip()
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
H,W = LI()
s = [S() for _ in range(H)]
ans = 1
for i,j in itertools.product(range(H),range(W)):
    if s[i][j]=='.':
        continue
    if i-1>=0 and s[i-1][j]=='#' or i+1<=H-1 and s[i+1][j]=='#':
        continue
    if j-1>=0 and s[i][j-1]=='#' or j+1<=W-1 and s[i][j+1]=='#':
        continue
    ans = 0
    break
print({1:'Yes',0:'No'}[ans])

Submission status: Accepted

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

canvas = [' ' + input() + ' ' for _ in range(H)]
canvas.insert(0, ' ' * (W + 2))
canvas.append(' ' * (W + 2))

for y in range(1, H + 1):
    for x in range(1, W + 1):
        if canvas[y][x] == '#':
            if '#' not in {canvas[y - 1][x], canvas[y + 1][x], canvas[y][x - 1], canvas[y][x + 1]}:
                print('No')
                break
    else:
        continue
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
import itertools,sys
def S(): return sys.stdin.readline().rstrip()
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
H,W = LI()
s = [S() for _ in range(H)]
ans = 1
for i,j in itertools.product(range(H),range(W)):
    if s[i][j]=='.':
        continue
    try:
        if s[i-1][j]=='#' or s[i+1][j]=='#' or s[i][j-1]=='#' or s[i][j+1]=='#':
            continue
    except:
        continue
    else:
        ans = 0
        break
print({1:'Yes',0:'No'}[ans])

Submission status: Accepted

Solution code:
import itertools,sys
def S(): return sys.stdin.readline().rstrip()
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
H,W = LI()
s = [S() for _ in range(H)]
ans = 1
for i,j in itertools.product(range(H),range(W)):
    if s[i][j]=='.':
        continue
    uh,uw,dh,dw = i-1,j,i+1,j
    lh,lw,rh,rw = i,j-1,i,j+1
    if not (0<=uh<=H-1) or not (0<=dh<=H-1) or not (0<=lh<=H-1) or not (0<=rh<=H-1):
        continue
    if not (0<=uw<=W-1) or not (0<=dw<=W-1) or not (0<=lw<=W-1) or not (0<=rw<=W-1):
        continue
    if s[uh][uw]=='#' or s[dh][dw]=='#' or s[lh][lw]=='#' or s[rh][rw]=='#':
        continue
    else:
        ans = 0
        break
print({1:'Yes',0:'No'}[ans])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

H, W = mi()
S = [input() for _ in range(H)]

dij = [[0, 1], [1, 0], [0, -1], [-1, 0]]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            for dj, di in dij:
                if -1 < i + di < H and -1 < j + dj < W:
                    if S[i + di][j + dj] == '#':
                        break
            else:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

H, W = mi()
S = [input() for _ in range(H)]

dij = [[0, 1], [1, 0], [0, -1], [-1, 0]]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            flag = 0
            for dj, di in dij:
                if -1 < i + di < H and -1 < j + dj < W:
                    if S[i + di][j + dj] == '#':
                        flag = 1
                        break
            if not flag:
                print('No')
                exit()
print('Yes')