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 sys
h,w=map(int,input().split())
l=[]

for i in range(h):
  s=[x for x in input()]
  l.append(s)

for i in range(w):
  im=max(i-1,0)
  ip=min(i+1,w-1)
  for j in range(h):
    jm=max(j-1,0)
    jp=min(j+1,h-1)
    
    if l[j][i]=="#":
      if l[jm][i]=="#" or l[jp][i]=="#" or l[j][ip]=="#" or l[j][im]=="#":
        continue
      else:
        print("No")
        sys.exit()
    else:
      continue
  
print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = []
for i in range(h):
  l = input()
  s.append(list(l))
ans = ''
for i in range(h):
  if ans == '':
    for j in range(w):
      if ans == '' and s[i][j] == '#':
        f = 0
        if i - 1 >= 0 and s[i - 1][j] == '#':
          f += 1
        if j - 1 >= 0 and s[i][j - 1] == '#':
          f += 1
        if j + 1 <= w - 1 and s[i][j + 1] == '#':
          f += 1
        if i + 1 <= h - 1 and s[i + 1][j] == '#':
          f += 1
        if f == 0:
          ans = 'No'
        else:
          next
      elif ans == '' and s[i][j] == '.':
        next
if ans == '':
  ans = 'Yes'
print(ans)

Submission status: Accepted

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

G=True
V=[(-1,0),(1,0),(0,-1),(0,1)]
for y in range(H+2):
    for x in range(W+2):
        if S[y][x]=="#":
            F=False
            for (a,b) in V:
                F|=(S[y+b][x+a]=="#")

            G&=F

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

Submission status: Accepted

Solution code:
import sys

H,W=map(int,input().split())
L=[[x for x in input()] for _ in range(H)]
ans='Yes'

for i in range(H):
  for j in range(W):
    if L[i][j]=='.':
      continue
    else:
      try:
        if L[i][j+1]=='#':
          continue
      except:
        pass
      try:
        if L[i][j-1]=='#':
          continue
      except:
        pass
      try:
        if L[i-1][j]=='#':
          continue
      except:
        pass
      try:
        if L[i+1][j]=='#':
          continue
      except:
        pass
      print('No')
      sys.exit()
      
print('Yes')

Submission status: Accepted

Solution code:
import numpy as np
H,W  = (int(X) for X in input().split())
Grid = np.ones((H+2,W+2),dtype=bool)
for TH in range(0,H):
    Grid[TH+1,1:W+1] = [False if X=='#' else True for X in input()] 

Flag = True
for TH in range(0,H):
    for TW in range(0,W):
        if Grid[TH+1,TW+1]==False:
            Check = np.all([Grid[TH+1,TW],Grid[TH+1,TW+2],Grid[TH,TW+1],Grid[TH+2,TW+1]])
            if Check==True:
                Flag = False
                break
    if Flag==False:
        break
if Flag:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
h, w = map(int, input().split())
s = [[c for c in l.strip()] for l in sys.stdin]

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

ans = "Yes"
for i in range(h):
    for j in range(w):
        if p[i][j] != "#":
            continue
        _ = False
        # x(j):-1
        if j-1 >= 0 and p[i][j-1] == "#":
            _ = True
        # x:+1
        if j+1 < w and p[i][j+1] == "#":
            _ = True
        # y(i):-1
        if i-1 >= 0 and p[i-1][j] == "#":
            _ = True
        # y:+1
        if i+1 < h and p[i+1][j] == "#":
            _ = True
        if _ is False:
            ans = "No"
            print(ans)
            sys.exit()

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

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

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


def eight_math_index(i, j, H, W):
    result = [(i-1, j),
              (i, j - 1),  (i, j + 1),
              (i + 1, j)]
    return list(filter(lambda x: (0 <= x[0] <= (H-1)) and (0 <= x[1] <= (W-1)), result))


for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            eight = eight_math_index(i, j, H, W)
            shuui_kuro_kazu = 0
            for (k, l) in eight:
                if S[k][l] == "#":
                    shuui_kuro_kazu += 1
            if shuui_kuro_kazu == 0:
                print("No")
                exit(0)

print("Yes")

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

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


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    H, W = il()
    S = [iss() for _ in range(H)]
    move = [[0, 1], [0, -1], [-1, 0], [1, 0]]

    for h in range(H):
        for w in range(W):
            if S[h][w] != '#': continue
            for mh, mw in move:
                if h + mh >= H or w + mw >= W: continue
                if S[h + mh][w + mw] == '#': break
            else:
                print('No')
                exit()
    else:
        print('Yes')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
l = []
for h in range(H):
    l.append(input())

j = True
for h in range(H):
    if not j:
        break
    for w in range(W):
        if l[h][w] == "#":
            j = False
            if h != 0:
                if l[h - 1][w] == "#":
                    j = True
            if h != H - 1:
                if l[h + 1][w] == "#":
                    j = True
            if w != 0:
                if l[h][w - 1] == "#":
                    j = True
            if w != W - 1:
                if l[h][w + 1] == "#":
                    j = True
            if not j:
                break
if j:
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

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

for i in range(h):
    a.append(input())

for k in range(h):
    for j in range(w):
        if a[k][j] == "#":
            for i in range(4):
                nx = j + dx[i]
                ny = k + dy[i]

                if nx < 0 or ny < 0 or nx >= w or ny >= h:
                    continue
                if a[ny][nx] == "#":
                    break
            else:
                flag = True
                print("No")
        if flag:
            break
    if flag:
        break
else:
    print("Yes")

Submission status: Accepted

Solution code:
# C - Grid Repainting 2
from collections import deque

H, W = map(int, input().split())
grid = [[''] * W for _ in range(H)]
for i in range(H):
    grid[i] = list(str(input()))

d = deque()
ans = 'Yes'
for i in range(H):
    for j in range(W):
        if grid[i][j] == '.':
            continue
        d.append((i, j))
        grid[i][j] = '.'
        tmp = 1
        while d:
            p, q = d.popleft()
            for x, y in ((0, 1), (1, 0), (-1, 0), (0, -1)):
                nx = p+x
                ny = q+y
                if nx < 0 or nx >= H or ny < 0 or ny >= W:
                    continue
                if grid[nx][ny] == '.':
                    continue
                d.append((nx, ny))
                grid[nx][ny] = '.'
                tmp += 1
        if tmp == 1:
            ans = 'No'
            break
    else:
        continue
    break
print(ans)

Submission status: Accepted

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

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

for i,row in enumerate(grid):
    for j in range(w):
        if grid[i][j] == "#":
            for k in range(4):
                nx = j + dx[k]
                ny = i + dy[k]            
                if (nx < 0) or (w-1 < nx) or (ny < 0) or (h-1 < ny):
                    continue
                elif grid[ny][nx] == "#":
                    break
                else:
                    pass
            else:
                print("No")
                sys.exit()
else:
    print("Yes")

Submission status: Accepted

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

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

for i in range(h):
    if ".#." in grid[i]:
        if i == 0:
            if grid[i+1][grid[i].find(".#.") + 1] != "#":
                print("No")
                exit()
        elif i == h-1:
            if grid[i-1][grid[i].find(".#.") + 1] != "#":
                print("No")
                exit()
        else:
            if grid[i-1][grid[i].find(".#.") + 1] != "#" and grid[i+1][grid[i].find(".#.") + 1] != "#":
                print("No")
                exit()

print("Yes")

Submission status: Accepted

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

flg = False
for y in range(h):
    for x in range(w):
        if f[y][x] == '#':
            cnt = 0
            if f[y+1][x] == f[y-1][x] == f[y][x+1] == f[y][x-1] == '.':
                flg = True

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

H,W = list(map(int,input().split()))
t = '.'*(W+2)
camp = [t]
for h in range(H):
    camp.append('.' + input().rstrip() + '.')
camp.append(t)

for h in range(1,H+1):
    for w in range(1,W+1):
        if camp[h][w] == '#':
            if camp[h][w-1] != '#' and camp[h][w+1] != '#' and camp[h-1][w] != '#' and camp[h+1][w] != '#':
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

H,W = list(map(int,input().split()))
t = '#'*(W+2)
camp = [t]
for h in range(H):
    camp.append('#' + input().rstrip() + '#')
camp.append(t)

for h in range(1,H+1):
    for w in range(1,W+1):
        if camp[h][w] == '#':
            if camp[h][w-1] != '#' and camp[h][w+1] != '#' and camp[h-1][w] != '#' and camp[h+1][w] != '#':
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

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

S_=[]
for h in range(H):
    for w in range(W):
        if S[h][w]=='.':
            continue
        try:
            S_left=S[h-1][w]
        except:
            S_left='.'
        try:
            S_right=S[h+1][w]
        except:
            S_right='.'
        try:
            S_up=S[h][w-1]
        except:
            S_up='.'
        try:
            S_down=S[h][w+1]
        except:
            S_down='.'
            
        S__=[S_left,S_right,S_up,S_down]
        S_.append(S__)

ans='Yes'
for s_ in S_:
    if '#' not in s_:
        ans='No'
        break
print(ans)

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
s = []
for i in range(H):
    s.append(list(input()))
can_paint_all = True
for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            continue
        if 0 <= i - 1 and s[i-1][j] == '#':
            continue
        if i + 1 < H and s[i+1][j] == '#':
            continue
        if 0 <= j - 1 and s[i][j-1] == '#':
            continue
        if j + 1 < W and s[i][j+1] == '#':
            continue
        can_paint_all = False
        break
    if not can_paint_all:
        break

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

Submission status: Accepted

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

HW = [list(input()) for i in range(H)]

def isPaintable(i, j):
    if i - 1 >=0 :
        if HW[i-1][j] == '#':
            return True
    if j - 1 >=0 :
        if HW[i][j-1] == '#':
            return True
    if i + 1 < H :
        if HW[i+1][j] == '#':
            return True
    if j + 1 < W :
        if HW[i][j+1] == '#':
            return True
    return False

isOk = True

for i in range(H):
    for j in range(W):
        if HW[i][j] == '#':
            if isPaintable(i,j):
                continue
            else:
                isOk = False
                break

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

Submission status: Accepted

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    h, w = i_map()
    str_list = s_row_list(h)
    
    for i in range(h):
        for j in range(w):
            if str_list[i][j] == '.':
                continue
            if i != 0:
                if str_list[i-1][j] == '#':
                    continue
            if j != 0:
                if str_list[i][j-1] == '#':
                    continue
            if i != h - 1:
                if str_list[i+1][j] == '#':
                    continue
            if j != w - 1:
                if str_list[i][j+1] == '#':
                    continue
            print('No')
            exit()

    print('Yes')

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
G = [list(input()) for _ in range(h)]
flg = True
for i in range(h):
    for j in range(w):
        if G[i][j] == "#":
            count = 1
            for u, v in ((0, 1), (1, 0), (-1, 0), (0, -1)):
                y, x = i + u, j + v
                if 0 <= x < w and 0 <= y < h:
                    # print(i, j, G[y][x])
                    if G[y][x] == "#":
                        count += 1
            if count < 2:
                flg = False
                break
    if not flg:
        break

print("Yes" if flg else "No")

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] == '.':
        continue
      flag = False
      if i > 0 and s[i-1][j] == '#':
        flag = True
      if i < h-1 and s[i+1][j] == '#':
        flag = True
      if j > 0 and s[i][j-1] == '#':
        flag = True
      if j < w-1 and s[i][j+1] == '#':
        flag = True
      if not flag:
        print('No')
        return
  print('Yes')
  return

if __name__ == "__main__":
  resolve()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

H, W = mapint()
query = [list(input()) for _ in range(H)]

def check():
    for h in range(H):
        for w in range(W):
            if query[h][w]=='.':
                continue
            if h!=0 and query[h-1][w]=='#':
                continue
            if h<H-1 and query[h+1][w]=='#':
                continue
            if w!=0 and query[h][w-1]=='#':
                continue
            if w<W-1 and query[h][w+1]=='#':
                continue
            return False
    return True

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

H,W = I()
maze = [list(s()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            F = 0
            if i != 0 :
                if maze[i-1][j] == '#':
                    F = 1
            if i != H-1:
                if maze[i+1][j] == '#':
                    F = 1
            if j != 0:
                if maze[i][j-1] == '#':
                    F = 1
            if j != W-1:
                if maze[i][j+1] == '#':
                    F = 1
            if not F:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys
h,w=map(int,input().split())
a=[]
for i in range(h):
  a.append(input())
for i in range(h):
  for j in range(w):
    if a[i][j]=='#':
      flag=1
      if i>0 and a[i-1][j]=='#':
        flag=0
      if j>0 and a[i][j-1]=='#':
        flag=0
      if i<h-1 and a[i+1][j]=='#':
        flag=0
      if j<w-1 and a[i][j+1]=='#':
        flag=0
      if flag==1:
        print('No')
        sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
S = ["."*(W+2)]
for _ in range(H):
    a = input().strip()
    a = "."+a+"."
    S.append(a)
S.append("."*(W+2))
flag = 0
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=="#":
            if S[i][j+1]=="." and S[i-1][j]=="." and S[i][j-1]=="." and S[i+1][j]==".":
                flag = 1
                break
    if flag==1:break
if flag==0:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
s = [list(input()) for _ in range(h)]
black = []
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            black.append([i,j])
for x,y in black:
    flag = False
    for a,b in [(0,1), (1,0), (0,-1), (-1,0)]:
        if [x+a, y+b] in black: flag = True
    if not flag:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

Solution code:
H, W = list(map(int,input().split()))
s = []
for i in range(H):  s.append(input())
def solve():
    for i in range(H):
        for j in range(W):
            if s[i][j] == "#":
                flg = 1
                if i >= 1:
                    if s[i - 1][j] == "#":
                        flg = 0
                if i < H - 1:
                    if s[i + 1][j] == "#":
                        flg = 0
                if j >= 1:
                    if s[i][j - 1] == "#":
                        flg = 0
                if j < W - 1:
                    if s[i][j + 1] == "#":
                        flg = 0
                if flg:  return "No"
    return "Yes"
ans = solve()
print(ans)

Submission status: Accepted

Solution code:
# 隣接マスにも黒が存在するか
def check(x, y):
    for i, j in zip(dirx, diry):
        nx = i + x
        ny = j + y
        if nx in range(h) and ny in range(w) and field[nx][ny] == '#':
            return True
    return False


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

dirx = [0, 1, 0, -1]
diry = [1, 0, -1, 0]

for i in range(h):
    for j in range(w):
        # そのマスが黒かチェック
        if field[i][j] == '#':
            if not check(i, j):
                print('No')
                exit()
print('Yes')

Submission status: Accepted

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

a = [["." for i in range(w + 2)] for j in range(h + 2)]
for i in range(h):
  for j in range(w):
    a[i + 1][j + 1] = temp[i][j]

for i in range(1, h + 2):
  for j in range(1, w + 2):
    if a[i][j] == "#":
      if a[i - 1][j] != "#" and a[i + 1][j] != "#" and a[i][j - 1] != "#" and a[i][j + 1] != "#":
        print("No")
        break
  else:
    continue
  break
else:
  print("Yes")

Submission status: Accepted

Solution code:
from collections import defaultdict
import itertools
import copy

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

h,w = readInts()
s = [list(input()) for i in range(h)]

for i in range(1,w-1):
  for j in range(1,h-1):
    if s[j][i]=="#":
      c = 0
      if s[j-1][i]=="#":
        c+=1
      if s[j+1][i]=="#":
        c+=1
      if s[j][i-1]=="#":
        c+=1
      if s[j][i+1]=="#":
        c+=1
      if c==0:
        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] == '#':
      
      if s[i+1][j]=='#' or s[i-1][j]=='#' or s[i][j-1]=='#' or s[i][j+1]=='#':
        continue
      else:
        print('No')
        exit()
      
print('Yes')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
zu=["."*(W+2)]

for i in range(H):
  zu.append("."+input()+".")

zu.append(zu[0])
#print(zu)

for h in range(1,H+2):
    for w in range(1,W+2):
        if zu[h][w]=="#":
          if zu[h-1][w]==".":
            if zu[h][w-1]==".":
              if zu[h+1][w]==".":
                if zu[h][w+1]==".":
                  print("No")
                  break
   
    else:
        continue
    break

else:
  print("Yes")
  
#if zu[h-1][w]=="." and zu[h][w-1]=="." and zu[h+1][w]=="." and zu[h][w+1]==".":

Submission status: Accepted

Solution code:
#ABC096 C

h, w = map(int, input().split())
l = list(input() for i in range(h))

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

ok = True
for i in range(h):
    for j in range(w):
        if l[i][j] == '#':

            flg = False
            for k in range(4):
                x = j + dx[k]
                y = i + dy[k]

                if x < 0 or x >= w or y < 0 or y >= h:
                    continue

                if l[y][x] == '#':
                    flg = True
            if flg == False:
                ok = False
                break
    if ok == False:
        break

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

Submission status: Accepted

Solution code:
from sys import exit

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

dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            flag = False
            for k in range(4):
                if 0 <= i + dx[k] < h and 0 <= j + dy[k] < w:
                    if s[i+dx[k]][j+dy[k]] == '#':
                        flag = True
            if not flag:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys
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] =="#":
            a=s[i][j-1]+s[i][j+1]+s[i-1][j]+s[i+1][j]
            if a.count("#") == 0:
                print("No")
                sys.exit()
else:
    print("Yes")

Submission status: Accepted

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

next_x = [1, 0, -1, 0]
next_y = [0, 1, 0, -1]

for y in range(h):
    for x in range(w):
        if S[y][x] == "#":
            for i in range(4):
                nx = x + next_x[i]
                ny = y + next_y[i]
                if 0 <= nx < w and 0 <= ny < h and S[ny][nx] == "#":
                    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 in range(h):
  for j in range(w):
    if s[i][j] == "#":
      if i != 0: # 上
        if s[i-1][j] == "#": continue
      if i != h-1: #下
        if s[i+1][j] == "#": continue
      if j != 0: #左
        if s[i][j-1] == "#": continue
      if j != w-1: #右
        if s[i][j+1] == "#": continue
      print("No")
      exit()
print("Yes")

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        if s[h][w] == '#':
            flag = False
            for dx, dy in ([-1, 0], [0, -1], [1, 0], [0, 1]):
                x = h + dx
                y = w + dy
                if x<0 or x>=H:
                    continue
                if y<0 or y>=W:
                    continue
                if s[x][y] == '#':
                    flag = True
                    break
            if not flag:
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
import sys
from itertools import product

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def nei(s, i, j, H, W):
    return (i+1 < H and s[i+1][j] == '#') \
        or (0 <= i-1 and s[i-1][j] == '#') \
        or (j+1 < W and s[i][j+1] == '#') \
        or (0 <= j-1 and s[i][j-1] == '#')


def main():
    H, W = mi()
    s = [input() for i in range(H)]
    for i in range(H):
        for j in range(W):
            if s[i][j] == '#' and not nei(s, i, j, H, W):
                print('No')
                return
    print('Yes')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 'Yes';
for i in range(h) :
    for j in range(w) :
        if s[i][j] == '.' :
            continue;
        elif i == 0 :
            if j == 0 :
                if s[i+1][j] == '.' and s[i][j+1] == '.' :
                    ans = 'No';
                    break;
            elif j == w-1 :
                if s[i+1][j] == '.' and s[i][j-1] == '.' :
                    ans = 'No';
                    break;
            else :
                if s[i+1][j] == '.' and s[i][j-1] == '.' and s[i][j+1] == '.' :
                    ans = 'No';
                    break;
        elif i == h-1 :
            if j == 0 :
                if s[i-1][j] == '.' and s[i][j+1] == '.' :
                    ans = 'No';
                    break;
            elif j == w-1 :
                if s[i-1][j] == '.' and s[i][j-1] == '.' :
                    ans = 'No';
                    break;
            else :
                if s[i-1][j] == '.' and s[i][j-1] == '.' and s[i][j+1] == '.' :
                    ans = 'No';
                    break;
        else :
            if j == 0 :
                if s[i-1][j] == '.' and s[i+1][j] == '.' and s[i][j+1] == '.' :
                    ans = 'No';
                    break;
            elif j == w-1 :
                if s[i-1][j] == '.' and s[i+1][j] == '.' and s[i][j-1] == '.' :
                    ans = 'No';
                    break;
            else :
                if s[i-1][j] == '.' and s[i+1][j] == '.' and s[i][j-1] == '.' and s[i][j+1] == '.' :
                    ans = 'No';
                    break;           


print(ans);