Problem:


Score: 300 points

Problem Statement

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

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

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

Determine if square1001 can achieve his objective.  

Constraints

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

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

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

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

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

Sample Input 2

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

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

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

Sample Output 3

Yes



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


Sample output:
Yes

Submission status: Accepted

Solution code:
import math
import string
import collections
from collections import Counter


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
h, w = map(int, input().split())
tmp = [None]*h
for i in range(h):
    tmp[i] = input()

for i in range(h):
    for j in range(w):
        if tmp[i][j] == '#':
            if (i != 0 and tmp[i-1][j] == '#')or(i != h-1 and tmp[i+1][j] == '#') or(j != 0 and tmp[i][j-1] == '#')or(j != w-1 and tmp[i][j+1] == '#'):
                continue
            print('No')
            exit()
print('Yes')
#H, W = map(int, input().split())
#s = [None]*H
# for i in range(H):
#    s[i] = input()
#    # print(s[i])
# for i in range(H):
#    for j in range(W):
#        if s[i][j] == '#':
#            #print(i, j)
#            if (i != 0 and s[i-1][j] == '#') or(i != H-1 and s[i+1][j] == '#')or(j != 0 and s[i][j-1] == '#') or(j != W-1 and s[i][j+1] == '#'):
#                continue
#            else:
#                print('No')
#                exit()
# print('Yes')

Submission status: Accepted

Solution code:
import math
import string
import collections
from collections import Counter


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
#h, w = map(int, input().split())
#tmp = [None]*h
# for i in range(w):
#    tmp[i] = input()
# print(tmp)
# for i in range(h):
#    for j in range(w):
#        if tmp[i][j] == '#':
#            if (i != 0 and tmp[i-1][j] == '#')or(i != h-1 and tmp[i+1][j] == '#') or(j != 0 and tmp[i][j-1] == '#')or(j != w-1 and tmp[i][j+1] == '#'):
#                continue
#
#            print('No')
#            exit()
# print('Yes')

H, W = map(int, input().split())
s = [None]*H
for i in range(H):
    s[i] = input()
    # print(s[i])
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            #print(i, j)
            if (i != 0 and s[i-1][j] == '#') or(i != H-1 and s[i+1][j] == '#')or(j != 0 and s[i][j-1] == '#') or(j != W-1 and s[i][j+1] == '#'):
                continue
            else:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
dxdy = ((0,1),(0,-1),(1,0),(-1,0))

H, W = map(int,input().split())
S = []

S.append('.'*(W+2))
for h in range(H):
    s = input()
    S.append('.'+s+'.')
S.append('.'*(W+2))

#print (S)

for h in range(1,H+1):
    for w in range(1,W+1):
        if S[h][w] == '.':
            continue
        ok = False
        for dx,dy in dxdy:
            if S[h+dx][w+dy] == '#':
                ok = True
                break
        if ok == False:
            print ('No')
            exit()
print ('Yes')

Submission status: Accepted

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

for _ in range(h):
    ma.append(list(str(input())))

add = ['.'] * w

ma.insert(0, add)
ma.insert(h+1, add)


for ad in range(h+1):
    ma[ad].insert(0, '.')
    ma[ad].insert(w+1, '.')

for high in range(1, h):
    for wid in range(1, w) :
        j_1 = False
        j_2 = False
        if ma[high][wid] == '#' :
            j_1 = True

        if j_1 == True :
            if ma[high][wid-1] == '#' or ma[high][wid+1] == '#' or ma[high-1][wid] == '#' or ma[high+1][wid] == '#' :
                j_2=True
            if j_2 == False :
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
# coding: utf-8
import re
import math
from collections import defaultdict
from collections import deque
import itertools
from copy import deepcopy
import random
import time
import os
import queue
import sys
import datetime
from functools import lru_cache
#@lru_cache(maxsize=None)
readline=sys.stdin.readline
sys.setrecursionlimit(2000000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_pow(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def get_primes(n,type="int"):
    if n==0:
        if type=="int":return []
        else:return [False]
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
    return ret
def seifu(x):
    return x//abs(x)
######################################################################################################
def main():
    H,W=map(int,input().split())
    S=[input() for a in range(H)]
    ans="Yes"
    for a in range(H):
        for b in range(W):
            if S[a][b]=="#":
                dx=[-1,1,0,0]
                dy=[0,0,-1,1]
                for c in range(4):
                    x=b+dx[c]
                    y=a+dy[c]
                    if x<0 or W<=x or y<0 or H<=y: continue
                    if S[y][x]=="#":break
                else:
                    ans="No"
    print(ans)
main()

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        if s[i][j]!="#":
            continue

        count=0
        for k in range(4):
            di=i+dy[k]
            dj=j+dx[k]

            if (di<0)|(di>h-1):
                continue
            if (dj<0)|(dj>w-1):
                continue

            if s[di][dj]=="#":
                count+=1
        if count==0:
            print("No")
            exit()

print("Yes")

Submission status: Accepted

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

flag = 1
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            if not i:
                if not j:
                    if s[i+1][j] == '.' and s[i][j+1] == '.':
                        flag = 0
                        break
                elif j == W - 1:
                    if s[i+1][j] == '.' and s[i][j-1] == '.':
                        flag = 0
                        break
                else:
                    if s[i+1][j] == '.' and s[i][j+1] == '.' and s[i][j-1] == '.':
                        flag = 0
                        break
            elif i == H - 1:
                if not j:
                    if s[i-1][j] == '.' and s[i][j+1] == '.':
                        flag = 0
                        break
                elif j == W - 1:
                    if s[i-1][j] == '.' and s[i][j-1] == '.':
                        flag = 0
                        break
                else:
                    if s[i-1][j] == '.' and s[i][j+1] == '.' and s[i][j-1] == '.':
                        flag = 0
                        break
            else:
                if not j:
                    if s[i+1][j] == '.' and s[i][j+1] == '.' and s[i-1][j] == '.':
                        flag = 0
                        break
                elif j == W - 1:
                    if s[i+1][j] == '.' and s[i][j-1] == '.' and s[i-1][j] == '.':
                        flag = 0
                        break
                else:
                    if s[i+1][j] == '.' and s[i][j+1] == '.' and s[i][j-1] == '.' and s[i-1][j] == '.':
                        flag = 0
                        break

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

Submission status: Accepted

Solution code:


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


# 配列への入力 簡単な書き方があった

field = []

for _ in range(h):
    ii = list(input())   ## inputのあとの()を忘れてた
    field.append(ii)


## 4方向の差分を表現する配列
dxdy = [[1,0],[0,1],[0,-1],[-1,0]]

result = 'Yes'
for i in range(h):
    for j in range(w):
        countOK = 0
        countNG = 0
        cell1 = field[i][j]

        if cell1 == '#':

            ## 4方向のうちどこかひとつでも黒があればOK
            ## ひとつも黒がなかったらNG
            for k in range(4):
                dx = dxdy[k][0]
                dy = dxdy[k][1]
                if i+dx <0:
                    continue
                if i+dx >h-1 :
                    continue
                if j+dy <0:
                    continue
                if j+dy >w-1:
                    continue

                cell2 = field[i+dx][j+dy]

                if cell2 == '#':
                    countOK += 1
                else:
                    countNG += 1

            
            if countOK == 0:
                result = 'No'


print(result)

Submission status: Accepted

Solution code:
def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    for r in range(H):
        for c in range(W):
            if S[r][c] == '.': continue
            bl = False
            if r > 0:
                bl |= (S[r - 1][c] == S[r][c])
            if r < H - 1:
                bl |= (S[r + 1][c] == S[r][c])
            if c > 0:
                bl |= (S[r][c - 1] == S[r][c])
            if c < W - 1:
                bl |= (S[r][c + 1] == S[r][c])
            if not bl:
                print('No')
                return
    print('Yes')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def examA():
    S = SI()
    T = SI()
    N = len(S)
    ans = "No"
    for i in range(N):
        cur = S[i:] + S[:i]
        #print(cur)
        if cur==T:
            ans = "Yes"
    print(ans)
    return

def examB():
    H, W = LI()
    S = [SI()for _ in range(H)]

    def judge(h, w, S):
        if h < H-1 and S[h+1][w]=="#":
            return True
        if h > 0 and S[h-1][w]=="#":
            return True
        if w < W-1 and S[h][w+1]=="#":
            return True
        if w > 0 and S[h][w-1]=="#":
            return True
        return False

    ans = "Yes"
    for h in range(H):
        for w in range(W):
            if S[h][w]=="#":
                if judge(h,w,S):
                    continue
                ans = "No"
    print(ans)
    return

def examC():
    L, R = LI()
    ans = inf
    if R-L>=3000:
        R = L+3000

    for l in range(L, R + 1):
        for r in range(l+1, R + 1):
            now = (l*r) %2019
            ans = min(ans,now)
    print(ans)
    return

def examD():
    T = {"dream", "dreamer", "erase", "eraser"}
    S = SI()
    ans = "NO"
    t = ""
    for s in S[::-1]:
        t += s
        if t[::-1] in T:
            t = ""
        if len(t)>10:
            print(ans)
            return
    if t=="":
        ans = "YES"
    print(ans)
    return

def examE():
    N = 50
    K = I()
    base = K//N
    A = [base+i for i in range(N)]
    rest = K%N
    for i in range(rest):
        A[-i-1] += 1
    print(N)
    print(" ".join(map(str,A)))
    return

def examF():
    N = 20
    C = [2**i for i in range(N)]
    L = I()
    cur = 1
    V = [[i+1, i+2, 0]for i in range(N-1)]
    now = 1
    while(now<N):
        if C[now]<=L:
            cur = C[now]
            V.append([now,now+1,C[now-1]])
            now += 1
        else:
            break
    #print(cur)

    for i in range(now)[::-1]:
        if C[i]<=L-cur:
            cost = cur
            V.append([i+1,N,cost])
            cur += C[i]

    print(N,len(V))
    for s,t,c in V:
        print(s,t,c)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examB()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Accepted

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

# 横
for i in range(H):
    for j in range(W - 1):
        if S[i][j] == '.':
            S_eval[i][j] = 0
        if S[i][j + 1] == '.':
            S_eval[i][j + 1] = 0
        if S[i][j] == S[i][j + 1] == '#':
            S_eval[i][j] = S_eval[i][j + 1] = 0
        
# 縦
for i in range(H - 1):
    for j in range(W):
        if S[i][j] == S[i + 1][j] == '#':
            S_eval[i][j] = S_eval[i + 1][j] = 0

sum_value = 0
for i in range(H):
    sum_value += sum(S_eval[i])
ans = 'No'
if sum_value == 0:
    ans = 'Yes'

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
#%%code
dxdy=[(1,0),(0,1),(-1,0),(0,-1)]
def resolve():
    H,W=pin()
    maze=[input() for h in range(H)]
    
    for i in range(H):
        for j in range(W):
            if maze[i][j]=="#":
                f=0
                for head in dxdy:
                    x,y=head
                    try:k=maze[i+x][j+y]
                    except IndexError:k=None
                    if k!=None and k=="#":
                        f=1
                if f==0:
                    print("No")
                    return
    print("Yes")
            
#%%submit!
resolve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


def read_map_as(H, replace={'#': 1, '.': 0}, pad=0):
    '''
    文字列のmapを置換して読み込み。デフォでは#→1,.→0
    '''
    # TODO paddingの機能入れたいね
    ret = [[pad] * (W + 2)]
    for _ in range(H):
        ret.append([pad] + [replace[s] for s in read()[:-1]] + [pad])
        # 内包表記はpypyでは若干遅いことに注意
        # #numpy使うだろうからこれを残しておくけど
    ret.append([pad] * (W + 2))
    return ret


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc096/tasks/abc096_c
# 普通に上下左右の連結成分が全部2以上か調べれば良い
# というか上下左右の連結成分がない場合に即時終了すれば良い
H, W = read_ints()
S = read_map_as(H)
for i, j in product(ra(1, H + 1), ra(1, W + 1)):
    if S[i][j] == 0:
        continue
    for di, dj in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if S[ni][nj] == 1:
            break
    else:
        print('No')
        exit()

print('Yes')

Submission status: Accepted

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

for i, a in enumerate(s):
    for j, b in enumerate(a):
        flg = False
        if b == "#":
            if 0 <= i - 1:
                if s[i-1][j] == "#":
                    flg = True
            if 0 <= j - 1:
                if s[i][j-1] == "#":
                    flg = True
            if i + 1 <= h-1:
                if s[i+1][j] == "#":
                    flg = True
            if j + 1 <= w-1:
                if s[i][j+1] == "#":
                    flg = True
        else:
            continue
        if not flg:
            print("No")
            exit()
else:
    print("Yes")

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
H,W = map(int,input().split())
Map = [["."]*(W+2)]
for _ in range(H):
    Map.append(["."]+list(input())[:-1] + ["."])
Map.append(["."]*(W+2))
flag = 0
for i in range(1,H+1):
    if flag == 1:
        break
    for j in range(1,W+1):
        if Map[i][j] == ".":
            continue
        if Map[i][j+1] == "." and Map[i][j-1] == "." and Map[i+1][j] == "." and Map[i-1][j] == ".":
            
            flag = 1
            break
if flag == 0:
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

h,w = iim()
l = []
l.append(['.']*(w+2))
for _ in range(h):
    l.append(['.']+list(input())+['.'])
l.append(['.']*(w+2))

flag = True
for i in range(1,h+1,):
    for j in range(1,w+1):
        if l[i][j] == '#':
            tmp = [l[i-1][j],l[i+1][j],l[i][j-1],l[i][j+1]]
            if '#' not in tmp:
                flag = False
print('Yes' if flag else 'No')

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    H, W = [int(item) for item in input().split()]
    grid = [[item for item in input()] for _ in range(H)]

    def chk(i,j):
        if grid[i][j] == '#':
            if 0 <= i-1:
                if grid[i-1][j] == '#':
                    return True
            if i +1 < H:
                if grid[i+1][j] == '#':
                    return True
            if 0 <= j -1:
                if grid[i][j-1] == '#':
                    return True
            
            if j + 1 < W:
                if grid[i][j+1] == '#':
                    return True
            
            return False
        else:
            return False

    for i in range(H):
        end_flag = False
        for j in range(W):
            # print(chk(i,j), grid[i][j])
            if grid[i][j] == '#':
                if chk(i,j) == True:
                    pass
                else:
                    print('No')
                    end_flag = True
                    break
        if end_flag:
            break
    else:
        print('Yes')

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

for i in range(h):
  for j in range(w):
    if s[i][j] == "#":
      ok = False
      for x,y in [(0,1), (0,-1), (1,0), (-1,0)]:
        new_i, new_j = i+x, j+y
        if new_i < 0 or new_i >= h or new_j < 0 or new_j >= w: continue
        if s[new_i][new_j] == "#": 
          ok = True
          break
      if not ok:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

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

for i in range(h-1):
  for j in range(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]=="#":
        pass
      else:
        print("No")
        sys.exit()
  
print("Yes")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
flag=False
count=0
for i in range(h):
  if flag==True:
    break
  for j in range(w):
    if s[i][j]=="#":
      count=0
      if i+1<=h-1 and s[i+1][j]==".":
        count+=1
      if 0<=i-1 and s[i-1][j]==".":
        count+=1
      if j+1<=w-1 and s[i][j+1]==".":
        count+=1
      if 0<=j-1 and s[i][j-1]==".":
        count+=1
    if count==4:
      flag=True
      break
if flag:
  print("No")
else:
  print("Yes")

Submission status: Accepted

Solution code:
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

h,w=MI()
lis=[[0 for i in range(w+2)] for j in range(h+2)]
for i in range(h):
    u=list(input().rstrip())
    #print(u)
    for j in range(w):
        if u[j]=="#":
            lis[i+1][j+1]=1
step=[[1,0],[-1,0],[0,1],[0,-1]]
#print(lis)
for i in range(1,h+1):
    for j in range(1,w+1):
        if lis[i][j]==1:
            count=0
            for x,y in step:
                if lis[i+x][j+y]==1:
                    count+=1
            if count==0:
                print("No")
                sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

h,w = inpl()
s = [input() for _ in range(h)]
di = [[1,0],[0,-1],[-1,0],[0,1]]
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            continue
        ok = False
        for y,x in di:
            ny = i + y
            nx = j + x
            if 0 <= ny < h and 0 <= nx < w:
                if s[ny][nx] == '#':
                    ok = True
        if not ok:
            print('No')
            quit()
print('Yes')

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
s = [input() for _ in range(H)]
ans = 'Yes'
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 = False
            for k in range(4):
                nx = j+dx[k]
                ny = i+dy[k]
                if nx < 0 or ny < 0 or nx >= W or ny >= H:
                    continue
                if s[ny][nx] == '#':
                    flag = True
            if not flag:
                ans = 'No'
                break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Map = [list(str(input())) for _ in range(H)]
dirc = ((0, 1), (1, 0), (-1, 0), (0, -1))
ans = 'Yes'
for h in range(H):
    for w in range(W):
        if Map[h][w]=='#':
            for y, x in dirc:
                if h+y<0 or h+y>=H or w+x<0 or w+x>=W:
                    continue
                if Map[h+y][w+x]=='#':
                    break
            else:
                ans='No'

print(ans)

Submission status: Accepted

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

for i in range(h):
  for j in range(w):
    if lst[i][j] == "#":
      if lst[max(0, i-1)][j] == lst[min(h-1, i+1)][j] == lst[i][max(0, j-1)] == lst[i][min(w-1, j+1)] == ".":
        print("No")
        exit()
        
print("Yes")

Submission status: Accepted

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

if __name__ == "__main__":
  resolve()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = []
for _ in range(h):
    s.append(input())
li = [(0,-1),(-1,0),(1,0),(0,1)]
for y in range(h):
    for x in range(w):
        if s[y][x]=='.':
            continue
        for dx, dy in li:
            if not (0<=x+dx<w and 0<=y+dy<h):
                continue
            if s[y+dy][x+dx]=='#':
                break
        else:
            print('No')
            break
    else:
        continue
    break
else:
    print('Yes')

Submission status: Accepted

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

ans = 'Yes'

for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            a = 0
            for dy,dx in [(1,0), (-1,0), (0,1), (0,-1)]:
                if 0 <= i+dy < H and 0 <= j+dx < W and s[i+dy][j+dx] == '#':
                    a = 1
            if a:
                continue
            ans = 'No'
print(ans)