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 = ['.'*(w+2)]
for i in range(h):
  ss = '.' + input() + '.'
  s.append(ss)
s.append('.'*(w+2))
dx = [1,0,-1,0]
dy = [0,1,0,-1]
for i in range(h):
  for j in range(w):
    cnt = 0
    for k in range(4):
      yi = i + dy[k]
      xi = j + dx[k]
      if s[i][j] == '#' and s[yi][xi] == '.':
        cnt += 1
    if cnt == 4:
      print('No')
      exit()

print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
lista=[]
ans=0
count=0
for i in range(h):
    lista.append(list(input()))
for i in range(h):
    for j in range(w):
        count=0
        if lista[i][j]=="#":
            if i!=0:
                if lista[i-1][j]=="#":
                    count=1
            if j!=0:
                if lista[i][j-1]=="#":
                    count=1
            if i!=h-1:
                if lista[i+1][j]=="#":
                    count=1
            if j!=w-1:
                if lista[i][j+1]=="#":
                    count=1
            if count==0:
                ans=1
                break
    if ans==1:
        break
if ans==1:
    print("No")
else:
    print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [input() for _ in range(h)] # #.のグリッドの場合
seen = [[0] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            seen[i][j] = 1
for i in range(h):
    for j in range(1, w):
        if s[i][j] == '#' and s[i][j-1] == '#':
            seen[i][j] = 0
            seen[i][j-1] = 0
for i in range(1, h):
    for j in range(w):
        if s[i-1][j] == '#' and s[i][j] == '#':
            seen[i-1][j] = 0
            seen[i][j] = 0

if sum(list(map(sum, seen))) > 0:
    print('No')
else:
    print('Yes')

Submission status: Accepted

Solution code:
import sys
h,w=map(int,input().split())
maze=[]
for i in range(h):
    maze.append(list(input()))
for i in range(h):
    for j in range(w):
        if maze[i][j]=="#":
            flg=False
            for l,m in [(0,1),(1,0),(-1,0),(0,-1)]:
                if 0<=i+l<h and 0<=j+m<w:
                    if maze[i+l][j+m]=="#":
                        flg=True
            if flg==False:
                print("No")
                sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
d=[['.']*(w+2) for j in range(h+2)]
for hh in range(h):
  moji=input()
  for ww in range(w):
    d[hh+1][ww+1]=moji[ww]
for k in range(1,h+1):
  for l in range(1,w+1):
    if d[k][l]=='#' and all(aa=='.' for aa in [d[k-1][l],d[k+1][l],d[k][l-1],d[k][l+1]]):
      print('No')
      exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def search4(y, x):
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    cnt = 0
    for k in range(4):
        ny = y + dy[k]
        nx = x + dx[k]
        if ny < 0 or H <= ny or nx < 0 or W <= nx:
            continue
        if field[ny][nx] == '#':
            cnt += 1
    return cnt

H, W = map(int, readline().split())
field = [readline().decode().rstrip() for _ in range(H)]

for y in range(H):
    for x in range(W):
        if field[y][x] == '#':
            if search4(y, x) == 0:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

s = [list(input()) for x in range(H)]

flg = True

for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            #上
            if 0 < i:
                if s[i-1][j] == '#':
                    flg = False
            #左
            if 0 < j:
                if s[i][j-1] == '#':
                    flg = False
            #右
            if j < W-1:
                if s[i][j+1] == '#':
                    flg = False
            #下
            if i < H-1:
                if s[i+1][j] == '#':
                    flg = False
            if flg:
                print('No')
                exit()
            flg = True
print('Yes')

Submission status: Accepted

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

s = [list(input()) for x in range(H)]

flg = True

for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            #上
            if 0 < i:
                if s[i-1][j] == '#':
                    flg = False
            #左
            if 0 < j:
                if s[i][j-1] == '#':
                    flg = False
            #右
            if j < W-1:
                if s[i][j+1] == '#':
                    flg = False
            #下
            if i < H-1:
                if s[i+1][j] == '#':
                    flg = False
            if flg:
                print('No')
                exit()
            flg = True
print('Yes')

Submission status: Accepted

Solution code:

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

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


def func(height, weight, board):
    for i in range(height):
        for j in range(weight):
            flg = False
            if board[i][j] == '#':
                if i > 0:
                    if board[i-1][j] == '#':
                        flg = True
                if j > 0:
                    if board[i][j-1] == '#' and not flg:
                        flg = True
                if j < weight - 1:
                    if board[i][j+1] == '#' and not flg:
                        flg = True
                if i < height - 1:
                    if board[i+1][j] == '#' and not flg:
                        flg = True
                if not flg:
                    print('No')
                    return
    else:
        print('Yes')


func(height=h, weight=w, board=s)

Submission status: Accepted

Solution code:
# For taking integer inputs.
import math
import sys


def inp():
    return(int(input()))


# For taking List inputs.
def inlist():
    return(list(map(int, input().split())))


# For taking string inputs. Actually it returns a List of Characters, instead of a string, which is easier to use in Python, because in Python, Strings are Immutable.
def instr():
    s = input()
    return(list(s[:len(s)]))


# For taking space seperated integer variable inputs.
def invr():
    return(map(int, input().split()))


def valid(x, y):
    if x >= 0 and x < row and y >= 0 and y < col:
        return True
    return False


fx = [0, 0, 1, -1]
fy = [1, -1, 0, 0]
row, col = invr()

a = []

for _ in range(row):
    a.append(instr())

for i in range(row):
    for j in range(col):

        if a[i][j] == "#":
            can_paint = 0
            for k in range(4):
                nx = i + fx[k]
                ny = j + fy[k]
                if valid(nx, ny) and a[nx][ny] == "#":
                    can_paint = 1
                    break
            if can_paint == 0:
                print("No")
                sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *
 
H, W = MAP()
s = [input() for _ in range(H)]

dy = (1, 0, -1, 0)
dx = (0, -1, 0, 1)

for x in range(W):
	for y in range(H):
		if s[y][x] =="#":
			for i in range(4):
				ny = y + dy[i]
				nx = x + dx[i]
				if 0 <= ny < H and 0 <= nx < W and s[ny][nx] == "#":
					break
			else:
				print("No")
				exit()

print("Yes")

Submission status: Accepted

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

ng = False
for i in range(h):
    for j in range(w):
        if M[i][j]  == '.':
            continue

        for mi, mj in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            ni = i + mi; nj = j + mj
            if ni < 0 or h <= ni or nj < 0 or w <= nj:
                continue

            if M[ni][nj] == '#':
                break
        else:
            ng=True
    if ng:
        break

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

Submission status: Accepted

Solution code:
import sys
h,w = map(int,input().split())
ls = ["0"*(w+2)]
ls += ["0" + str(input()) + "0" for _ in range(h)]
ls += ["0"*(w+2)]
for i in range(1,h+1):
    for j in range(1,w+1):
        if ls[i][j] == "#":
            if ls[i-1][j] != "#" and ls[i+1][j] != "#" and ls[i][j-1] != "#" and ls[i][j+1] != "#":
                print("No")
                sys.exit()
print("Yes")

Submission status: Accepted

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

ans='Yes'
h=1
while h<=H:
    w=1
    while w<=W:
        if hw[h][w]=='#':
            if all([hw[h-1][w]=='.',hw[h+1][w]=='.',hw[h][w-1]=='.',hw[h][w+1]=='.']):
                ans='No'
        w+=1
    h+=1

print(ans)

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]=="#":
            if (i>0 and s[i-1][j]=="#") or (i<h-1 and s[i+1][j]=="#") or (j<w-1 and s[i][j+1]=="#") or (j>0 and s[i][j-1])=="#":
                pass
            else:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
def is_valid(s, i, j):
    if s[i][j] == '.':
        return True

    if i > 0 and s[i-1][j] == '#':
        return True

    if j > 0 and s[i][j-1] == '#':
        return True

    if i < len(s) - 1 and s[i+1][j] == '#':
        return True

    if j < len(s[i]) - 1 and s[i][j+1] == '#':
        return True

    return False


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 not is_valid(s, i, j):
            print('No')
            exit()

print('Yes')

Submission status: Accepted

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

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

for i in range(H):
  for j in range(W):
    ok=0
    if S[i][j]=='#':
      for k in range(4):
        x=j+dx[k]
        y=i+dy[k]
        if 0<=x<W and 0<=y<H and S[y][x]=='#':
          ok=1
          break
      if ok==0:
        print('No')
        exit()
print('Yes')

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, input().split())
    s = [list(input()) for _ in range(h)]
    
    def judge(y, x):
        if s[y][x] == '.':
            return True
        for dx, dy in [[-1, 0], [0, -1], [1, 0], [0, 1]]:
            ny = y + dy
            nx = x + dx
            if 0<= ny < h and 0<= nx < w:
                if s[ny][nx] == '#':
                    return True
        return False
            
        
    for y in range(h):
        for x in range(w):
            if judge(y, x) == False:
                print('No')
                return
    else:
        print('Yes')
        
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
sheet = []
flame = ""
for i in range(W+ 2):
  flame+= "."

sheet.append(flame)  
for i in range(H):
  row = "."+ input()+ "."
  sheet.append(row)
  
sheet.append(flame)

flag = 1
for i in range(1, H+ 1):
  for j in range(1, W+ 1):
    if(sheet[i][j] == "#"):
      if(sheet[i- 1][j] != "#" and sheet[i][j- 1] != "#" and \
        sheet[i+ 1][j] != "#" and sheet[i][j+ 1] != "#"):
        flag = 0
        print("No")
        break
  if(flag == 0):
    break

if(flag == 1):
  print("Yes")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
field = [input() for _ in range(H)]
drawable = True
for i in range(H):
    for j in range(W):
        if field[i][j] == ".":
            continue
        for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            ni, nj = i + di, j + dj
            if 0 <= ni < H and 0 <= nj < W and field[ni][nj] == "#":
                break
        else:
            drawable = False
print("Yes") if drawable else print("No")

Submission status: Accepted

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

ans="Yes"
dero=False
for i in range(1,H+1):
  for j in range(1,W+1):
    if a[i][j]==".":
      continue
    f1=a[i-1][j]=="."
    f2=a[i+1][j]=="."
    f3=a[i][j+1]=="."
    f4=a[i][j-1]=="."
    if f1 and f2 and f3 and f4:
      ans="No"
      dero=True
      break
  if dero:
    break
print(ans)

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
original = [list(input()) for _ in range(h)]
for i in range(h-1):
  for j in range(w-1):
    if original[i][j]=='#' and original[i-1][j]!='#' and original[i+1][j]!='#' and original[i][j-1]!='#' and original[i][j+1]!='#':
      print('No')
      exit()
      
print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a=['.'*(w+2)]
for i in range(h):
  a.append('.'+input()+'.')
a.extend(['.'*(w+2)])
flg=0
for i in range(1,h+1):
  for j in range(1,w+1):
    if a[i][j] == '#':
      if a[i-1][j] == '.' and a[i][j-1] == '.' and a[i+1][j] == '.' and a[i][j+1] == '.':
        flg = 1
if flg == 0:
  print('Yes')
else:
  print('No')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
field = [input() for i in range(h)]
DXY = [(1, 0), (0, 1), (-1, 0), (0, -1)]
for y in range(h):
	for x in range(w):
		if field[y][x] == '#':
			flag = False
			for dy, dx in DXY:
				ny = y + dy
				nx = x + dx
				if not (0 <= ny < h and 0 <= nx < w):
					continue
				if field[ny][nx] == '#':
					flag = True
					break
			if flag == False:
				print("No")
				exit()
else:
	print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
grid = [list(input()) for _ in range(H)]
direction = (-1, 1)
H -= 1
W -= 1
for h, line in enumerate(grid):
    for w, g in enumerate(line):
        if g == '.':
            continue
        pos = ((h - 1, w), (h + 1, w), (h, w - 1), (h, w + 1))
        for y, x in pos:
            if y < 0 or H < y:
                continue
            if x < 0 or W < x:
                continue
            if grid[y][x] == '#':
                break
        else:
            print('No')
            break
    else:
        continue
    break
else:
    print('Yes')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=["."*(W+2)]+["."+input()+"." for i in range(H)]+["."*(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]=="." 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 = [int(i)  for i in input().split()]

listHW = []
flag = False
for i in range(0, h):
  listHW.append(list(input()))

  
for i in range(0, len(listHW)):
  if flag:
    break
  listw = listHW[i]
  #print("listw", listw)
  for ii in range(0, len(listw)):
    w = listw[ii]
    if w == "#":
      #print(i, ii)
    
      if i == 0:
        #右上
        if ii == len(listw)-1:
          if w == listHW[i+1][ii] or w == listw[ii-1]:
            continue
        #左上
        elif ii == 0:
          if w == listHW[i+1][ii] or w == listw[ii+1]:
            continue 
        #上段
        elif w == listw[ii+1] or w == listw[ii-1] or w == listHW[i+1][ii]:
          continue

      elif i == len(listHW)-1:
        #右下
        if ii == len(listw)-1:
          if w == listHW[i-1][ii] or w == listw[ii-1]:
            continue
        #左下
        elif ii == 0:
          if w == listHW[i-1][ii] or w == listw[ii+1]:
            continue 
        #下段
        elif w == listw[ii+1] or w == listw[ii-1] or w == listHW[i-1][ii]:
          continue
      
      #左列
      elif ii == 0:
        if w == listw[ii+1]  or w == listHW[i-1][ii] or w == listHW[i+1][ii]:
          continue
      #右列
      elif ii == len(listw)-1:
        if w == listw[ii-1]  or w == listHW[i-1][ii] or w == listHW[i+1][ii]:
          continue
              
      #通常
      elif w == listw[ii-1] or w == listw[ii+1]or w == listHW[i-1][ii] or w == listHW[i+1][ii]:
        continue

      #print("break後")
      #隣接するものがなかった時点で終了      
      flag = True
      break
    
    #「.」だったときはとばす
    else:
      continue
    



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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu May 28 22:59:47 2020

@author: naoki
"""
import sys

H,W = map(int,input().split()) # 行H列Wを入力
square = [input() for i in range(H)] # マス目の目標
zero_input = [str(0) for i in range(W+2)] # マス目外を0で埋める
zero_row1 = "".join(zero_input) #W+2文字0の文字列を作成
zero_row2 = [zero_row1]

for i in range(H):
    square[i] = "0" + square[i] + "0" # 各行の最初と最後に０を追加

square = zero_row2 + square + zero_row2 # 最初と最後の行に0のみの文字列を挿入

dy = [-1,0,0,1] # 横方向（列）の変位
dx = [0,-1,1,0] #　縦方向（行）の変位

for i in range(1,H+1):
    for j in range(1,W+1):
        if square[i][j] == "#": #  "#"ならば黒に塗る
            count = 0
            for k in range(4):
                if square[i+dx[k]][j+dy[k]] == "#" and count == 0: 
                    count+=1
            if count ==0:
                print("No")
                sys.exit()
print("Yes")

Submission status: Accepted

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

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

di=[1,-1,0,0]
dj=[0,0,1,-1]
for i in range(H):
    for j in range(W):
        if s[i][j]=='#':
            found=False
            for k in range(4):
                i2=i+di[k]
                j2=j+dj[k]
                if 0<=i2 and i2<H and 0<=j2 and j2<W:
                    if s[i2][j2]=='#':
                        found=True
            if not found:
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
n,m = map(int,input().split())

s=[list(str(input())) for _ in range(n)]

di=[1,-1,0,0]
dj=[0,0,1,-1]
for i in range(n):
    for j in range(m):
        if s[i][j]=='#':
            found=False
            for k in range(4):
                i2=i+di[k]
                j2=j+dj[k]
                if 0<=i2 and i2<n and 0<=j2 and j2<m:
                    if s[i2][j2]=='#':
                        found=True
            if not found:
                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 in range(1, h-1):
    for j in range(1, w-1):
        if s[i][j] == '#' and s[i - 1][j] != '#' and s[i][j - 1] != '#' and s[i + 1][j] != '#' and s[i][j + 1] != '#':
            print('No')
            exit()
print('Yes')

Submission status: Accepted

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

s = []
for _ in range(h):
    s.append(list(input()))
    
direction = [[0, 1], [0, -1], [1, 0], [-1, 0]]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            for dy, dx in direction:
                y = i + dy
                x = j + dx
                try:
                    if s[y][x] == "#":
                        break
                except IndexError:
                    pass
            else:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

DX = [-1,0,1,0]
DY = [0,1,0,-1]
for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            count = 0
            for dx,dy in zip(DX, DY):
                if H-1>=h-dy >= 0 and W-1>=w-dx >= 0 and S[h-dy][w-dx] == ".":
                    count += 1
            if count == 4:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
grid = []
flag = 1
for h in range(H):
  S = input()
  tmp = []
  for s in S:
    tmp.append(s)
  grid.append(tmp)

for h in range(H):
  for w in range(W):
    mark = grid[h][w]
    if mark == ".": continue

    if h-1 >= 0:
      up = grid[h-1][w]
    else: up = "."

    if h+1 <= H-1:
      down = grid[h+1][w]
    else: down = "."

    if w-1 >= 0:
      left = grid[h][w-1]
    else: left = "."
    
    if w+1 <= W-1:
      right = grid[h][w+1]
    else: right = "."

    if (up != "#") & (down != "#") & (right != "#") & (left != "#"): 
      flag = 0
      break

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

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
H,W = map(int,input().split())
S = [input().rstrip() for _ in range(H)]

for h in range(H):
    for w in range(W):
        # 白マスなら次へ
        if S[h][w]==".":
            continue
        # 黒マスなら上下左右に黒マスが1つでもあれば ok, なければ ng
        flg = False
        for dh,dw in [(1,0), (-1,0), (0,1), (0,-1)]:
            nh = h+dh
            nw = w+dw
            if 0 <= nh < H and 0 <= nw < W:
                if S[nh][nw] == "#":
                    flg = True
                    break
        if not flg:
            print("No")
            sys.exit()

print("Yes")

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if table[i][j] == "#":
            if i - 1 >= 0 and table[i-1][j] == "#":
                ok = True
            elif i + 1 <= h-1 and table[i+1][j] == "#":
                ok = True
            elif j - 1 >= 0 and table[i][j-1] == "#":
                ok = True
            elif j + 1 <= w-1 and table[i][j+1] == "#":
                ok = True
            else:
                ok = False
            if ok == False:
                 print("No")
                 exit()
print("Yes")

Submission status: Accepted

Solution code:
import sys

a = sys.stdin.readline().split(' ')
data = sys.stdin.readlines()

S = int(a[0])
H = int(a[1])

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

for i in range(S):
    for j in range(H):
        count = 0
        if data[i][j] == '#':
            for k in range(4):
                x = j + dx[k]
                y = i + dy[k]
                if x < 0 or y < 0 or x >= H or y >= S:
                    continue
                else:
                    if data[y][x] == "#":
                        count = count+1
            if count == 0:
                print('No')
                sys.exit()


print('Yes')

Submission status: Accepted

Solution code:
def surrounded_by_pound(grid, i, j):
  return '#' in (grid[i-1][j], grid[i+1][j], grid[i][j-1], grid[i][j+1])


def paintable(grid):
  for i in range(1, H+1):
    for j in range(1, W+1):
      if grid[i][j] == '#' and not surrounded_by_pound(grid, i, j):
        return False
  return True



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

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

print('Yes') if paintable(grid) else print('No')

Submission status: Accepted

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

Submission status: Accepted

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

m = []
for _ in range(H):
    line = list(input())
    m.append(line)

for y in range(H):
    for x in range(W):
        if m[y][x]==".":
            continue
        if y+1<H and m[y+1][x]=="#":
            continue

        if y-1>-1 and m[y-1][x]=="#":
            continue

        if x+1<W and m[y][x+1]=="#":
            continue

        if x-1>-1 and m[y][x-1]=="#":
            continue
        print("No")
        exit()

print("Yes")

Submission status: Accepted

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

for i in range(h):
    tmp = list(input())
    for j in range(1, w+1):
        s[i][j] = tmp.pop()

for i in range(1, h+1):
    for j in range(1, w+1):
        up = s[i-1][j]
        down = s[i+1][j]
        left = s[i][j-1]
        right = s[i][j+1]

        if  s[i][j] == "#" and s[i][j] != up and s[i][j] != down and s[i][j] !=left and s[i][j] != right:
            print("No")
            exit()

print("Yes")

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            continue
        
        if 0 <= i-1:
            up = s[i-1][j]
        else:
            up = s[i][j]
        
        if i+1 < h:
            down = s[i+1][j]
        else:
            down = s[i][j]

        if 0 <= j-1:
            left = s[i][j-1]
        else:
            left = s[i][j]
        
        if j+1 < w:
            right = s[i][j+1]
        else:
            right = s[i][j]
        
        if s[i][j] != up and s[i][j] != down and s[i][j] !=left and s[i][j] != right:
            print("No")
            exit()

print("Yes")

Submission status: Accepted

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

for i in range(H):
  S = list(input())
  arr.append(S)
  
ck = 0
for i in range(H):
  for j in range(W):
    if arr[i][j] == "#":
      if i != 0:
        a = arr[i-1][j]
      else:
        a = "."
      if i != H-1:
        b = arr[i+1][j]
      else:
        b = "."
      if j != 0:
        c = arr[i][j-1]
      else:
        c = "."
      if j != W-1:
        d = arr[i][j+1]
      else:
        d = "."

      if a == "." and b == "." and c == "." and d == ".":
        ck = 1
      
if ck == 1:
  print("No")
else:
  print("Yes")

Submission status: Accepted

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

for i in range(1, h+1):
	s[i] = "." + input() + "."

for i in range(h+2):
	for j in range(w+2):
		if s[i][j] == "#":
			setroad.add((i,j))
			road.append((i,j))

def isnotland(n, m):
	if not (n, m) in setroad:
		return True
	else:
		return False

ans = 1

for x, y in road:
	if isnotland(x, y+1) and isnotland(x, y-1) and isnotland(x-1, y) and isnotland(x+1, y):
		ans = 0

print(["No","Yes"][ans])

Submission status: Accepted

Solution code:
from collections import deque
def make_wall(maze,h,w):
    maze=deque(maze)
    for i in range(h):
        maze[i]="."+maze[i]+"."
    maze.appendleft("."*(w+2))
    maze.append("."*(w+2))
    return list(maze)

h,w=map(int,input().split())
s=[input() for _ in range(h)]
s=make_wall(s,h,w)
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
    else:
        continue
    break
print(ans)

Submission status: Accepted

Solution code:
h, w=map(int, input().split())
s=['.'*(w+2)]+['.'+input()+'.' for _ in range(h)]+['.'*(w+2)]
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]):
        print('No')
        exit()
print('Yes')