Problem:


Score: 300 points

Problem Statement

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

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

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

Determine if square1001 can achieve his objective.  

Constraints

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

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

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

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

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

Sample Input 2

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

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

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

Sample Output 3

Yes



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


Sample output:
Yes

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
s = list()
t = list()
a = [0 for i in range(w+2)]
s.append(a)
for i in range(h):
    b = str(input())
    c = list(('0'+b+'0').replace('.','0').replace('#','1'))
    d = list(map(int, c))
    s.append(d)
    t.append(b)
s.append(a)
flag = 0

for j in range(1,h+1):
    for k in range(1,w+1):
        if flag == 1:
            break
        if s[j][k] == 1:
            cnt = 0
            cnt = s[j-1][k] + s[j][k-1] + s[j][k+1] + s[j+1][k] 
            if cnt == 0:
                flag = 1
                print('No')

if flag == 0:
    print('Yes')

Submission status: Accepted

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

jougesayuu = [(-1,0),(1,0),(0,-1),(0,1)]

check = True
for i in range(H):
  for j in range(W):
    if s[i][j] == "#":
      flag = False
      for (n,m) in jougesayuu:
        ni, nj = i + n, j + m
        if ni < 0 or H <= ni or nj < 0 or W <= nj:
          continue
        if s[ni][nj] == "#":
          flag = True
      if not flag:
        check = False
        
print("Yes" if check else "No")

Submission status: Accepted

Solution code:
H, W = map(lambda x:int(x)+2, input().split())
grid = "."*W
for _ in range(H-2):
  grid += "."+input()+"."
grid += "."*W

Flag = True
move_list = [1, -1, W, -W]

for i in range(1, H-1):
  for j in range(1, W-1):
    if grid[i*W+j] == "#" and all(grid[i*W+j+move] == "." for move in move_list):
      Flag = False
      break

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

Submission status: Accepted

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

import sys
for j in range(1,H-1):
    for i in range(1,W-1):
        if s[j][i] =='#':
            if s[j][i-1] == '#':
                pass
            elif s[j-1][i] == '#':
                pass
            elif s[j][i+1] == '#':
                pass
            elif s[j+1][i] == '#':
                pass
            else:
                print('No')
                sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
MOD = 10**9+7

def mint():
    return map(int,input().split())
def lint():
    return map(int,input().split())
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)

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

def f(i,j):
    ans = False
    for x,y in [(i+1,j), (i-1,j), (i,j-1), (i,j+1)]:
        if 0<=x<H and 0<=y<W and S[x][y]=='#':
            ans = True
            break
    return ans

for i in range(H):
    for j in range(W):
        if S[i][j]=='#':
            if not f(i,j):
                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] == "#":
            c = 0
            for ni, nj in [(i+1, j),(i, j+1),(i-1, j),(i, j-1)]:
                if 0 <= ni < h and 0 <= nj < w and s[ni][nj] == "#":
                    c += 1
            if c == 0:
                print("No")
                exit(0)
print("Yes")

Submission status: Accepted

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

ans=[[0]*w for i in range(h)]

for i in range(h):
  for j in range(w):
    x,y=0,0
    if lis[i][j]=="#":
      ans[i][j]=1
      for a in range(-1,2):
        if 0<=i+a<h and lis[i+a][j]=="#" and a!=0:
          x+=1
      ans[i][j]+=x
      for b in range(-1,2):
        if 0<=j+b<w and lis[i][j+b]=="#" and b!=0:
          y+=1
      ans[i][j]+=y
      if ans[i][j]==1:
        print("No")
        exit()
    else:
      ans[i][j]=0
      
print("Yes")

Submission status: Accepted

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

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


def solve(H: int, W: int, S: "List[str]"):
    for i in range(H):
        for j in range(W):
            if S[i][j] == ".":
                continue
            if i - 1 >= 0:
                if S[i - 1][j] == "#":
                    continue
            if i + 1 < H:
                if S[i + 1][j] == "#":
                    continue
            if j - 1 >= 0 and S[i][j - 1] == "#":
                continue
            if j + 1 < W and S[i][j + 1] == "#":
                continue
            print(NO)
            return
    print(YES)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    S = [list(next(tokens)) for _ in range(H)]  # type: "List[str]"
    solve(H, W, S)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
dx=[0,1,0,-1] ; dy=[1,0,-1,0]
A=[list(input()) for i in range(h)]

for i in range(h):
    for  j in range(w):
        if A[i][j]=="#":
            for q,e in zip(dx,dy):
                if 0<=i+q<h and 0<=j+e<w:
                    if A[i+q][e+j]=="#":break
            else:
                print("No");exit()
print("Yes")

Submission status: Accepted

Solution code:
from sys import stdin
def input():
    return stdin.readline().strip()

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

def check(x, y, up, down, left, right):
    if s[x][y] == '.':
        return 0
    if up and s[x-1][y] == '#':
        return 0
    if down and s[x+1][y] == '#':
        return 0
    if left and s[x][y-1] == '#':
        return 0
    if right and s[x][y+1] == '#':
        return 0
    else:
        print('No')
        exit()

# (0, y)
check(0, 0, False, True, False, True)
for y in range(1, w-1):
    check(0, y, False, True, True, True)
check(0, w-1, False, True, True, False)

# (x, y)
for x in range(1, h-1):
    check(x, 0, True, True, False, True)
    for y in range(1, w-1):
        check(x, y, True, True, True, True)
    check(x, w-1, True, True, True, False)

# (h-1, y)
check(h-1, 0, True, False, False, True)
for y in range(1, w-1):
    check(h-1, y, True, False, True, True)
check(h-1, w-1, True, False, True, False)

print('Yes')

Submission status: Accepted

Solution code:
from itertools import product

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

def dfs(p):
    if s[p[0]][p[1]] == '.':  # p[0]gatate , p[1] gayoko
        return None

    temp = [1 if s[p[0]+y][p[1]+x] == '#' else 0 for (x, y) in [[1, 0], [0, 1], [0, -1], [-1, 0]] if 0 <= y+p[0] < h and 0 <= x+p[1] < w]
    if sum(temp) <= 0:
        print('No')
        exit(0)


for i in range(len(s)):
    for j in range(len(s[0])):
        dfs([i, j])
print('Yes')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
List = [list(input()) for i in range(H)]
res = "Yes"
flag = True

def checkAround(ListX,i,j):
  if i == 0 and j ==0:
    if List[i+1][j] == "#" or List[i][j+1] == "#":
      return True
    else: 
      return False
  elif i == 0 and j !=0 and j != W-1:
    if List[i+1][j] == "#" or List[i][j-1] == "#" or List[i][j+1] == "#":
      return True
    else: 
      return False
  elif i == 0 and j == W-1:
    if List[i+1][j] == "#" or List[i][j-1] == "#":
      return True
    else: 
      return False
  elif i != 0 and i != H-1 and j == 0:
    if List[i+1][j] == "#" or List[i-1][j] == "#" or List[i][j+1] == "#":
      return True
    else: 
      return False
  elif i == H-1 and j == W-1:
    if List[i-1][j] == "#" or List[i][j-1] == "#":
      return True
    else: 
      return False
  elif i == H-1 and j !=0 and j != W-1:
    if List[i-1][j] == "#" or List[i][j+1] == "#" or List[i][j-1] == "#":
      return True
    else: 
      return False
  elif i == H-1 and j == 0:
    if List[i-1][j] == "#" or List[i][j-1] == "#":
      return True
    else: 
      return False
  elif i != 0 and i != H-1 and j == W-1:
    if List[i+1][j] == "#" or List[i-1][j] == "#" or List[i][j-1] == "#":
      return True
    else: 
      return False
  else:
    if List[i+1][j] == "#" or List[i-1][j] == "#" or List[i][j-1] == "#" or List[i][j+1] == "#":
      return True
    else: 
      return False

for k in range(H):
  for l in range(W):
    if List[k][l] == ".":
      pass
    else:
      flag = checkAround(List,k,l)
      if not flag:
        res = "No"
        break
  if not flag:
    break
print(res)

Submission status: Accepted

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

import sys
for j in range(1,H-1):
    for i in range(1,W-1):
        if s[j][i] =='#':
            if s[j][i-1] == '#':
                pass
            elif s[j-1][i] == '#':
                pass
            elif s[j][i+1] == '#':
                pass
            elif s[j+1][i] == '#':
                pass
            else:
                print('No')
                sys.exit()
print('Yes')

Submission status: Accepted

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

ans = "Yes"
for i, s in enumerate(sl):
    for j, p in enumerate(s):
        if sl[i][j] == ".":
            continue
        up_s = sl[i - 1][j] if i - 1 >= 0 else ""
        down_s = sl[i + 1][j] if i + 1 < H else ""
        left_s = sl[i][j - 1] if j - 1 >= 0 else ""
        right_s = sl[i][j + 1] if j + 1 < W else ""
        if "#" in [up_s, down_s, left_s, right_s]:
            continue
        ans = "No"
        break
    if ans == "No":
        break

print(ans)

Submission status: Accepted

Solution code:
import sys
H,W = map(int,input().split())
lsHW = [['.' for i in range(W+2)]]
for i in range(H):
    Sls = ['.'] + list(input()) + ['.']
    lsHW.append(Sls)
lsHW.append(['.' for i in range(W+2)])
for i in range(1,H+1):
    for j in range(1,W+1):
        if lsHW[i][j] == '.':
            continue
        else:
            if not (lsHW[i-1][j] == '#' or lsHW[i+1][j] == '#' or lsHW[i][j-1] == '#' or lsHW[i][j+1] == '#'):
                print('No')
                sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def main():
    H, W = map(int, input().split())
    Grid = [list(input()) for _ in range(H)]

    uf = UnionFind(H * W)
    direction = [(1, 0), (-1, 0), (0, 1), (0, -1)]

    for i in range(H):
        for j in range(W):
            if Grid[i][j] == '#':
                for dx, dy in direction:
                    nx, ny = i + dx, j + dy
                    if 0 <= nx <= H - 1 and 0 <= ny <= W - 1:
                        if Grid[nx][ny] == '#':
                            uf.union(i * W + j, nx * W + ny)
    ans = True

    for k, v in uf.all_group_members().items():
        i, j = k // W, k % W
        if Grid[i][j] == '#' and len(v) == 1:
            ans = False
            break

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

d = '.'
dn = '#'

def isPaintable(h, w, x, y, canvas):
    if x > 0:
        if canvas[y][x-1] == dn:
            return True
    if x < w-1:
        if canvas[y][x+1] == dn:
            return True
    if y > 0:
        if canvas[y-1][x] == dn:
            return True
    if y < h-1:
        if canvas[y+1][x] == dn:
            return True
    return False

def main():
    ans = 'Yes'
    h, w = map(int, input().split())
    canvas = []
    for _ in range(h):
        s = list(input().rstrip('\n'))
        canvas.append(s)
    for y in range(h):
        if ans == 'No':
            break
        for x in range(w):
            if canvas[y][x] == dn and not isPaintable(h, w, x, y, canvas):
                ans = 'No'
                break
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

# '#'が縦横にあったらfalse
def sirokuro(i,j):
    result = True
    if i + 1  < h :
        if s[i + 1][j] == '#':
            result = False
    if i - 1 >= 0:
        if s[i - 1][j] == '#':
            result = False
    if j + 1 < w:

        if s[i][j + 1] == '#':

            result = False
    if j - 1 >= 0:
        if s[i][j - 1] == '#':
            result = False
    return result

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            if sirokuro(i,j):
                print('No')
                sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
from scipy.special import comb
import copy
sys.setrecursionlimit(10**6)


def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


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


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


H, W = zz()
board = []
for i in range(H):
    board.append(S())

dx_lis = (-1, 0, 1, 0)
dy_lis = (0, -1, 0, 1)


def check(i, j):
    for dx, dy in zip(dx_lis, dy_lis):
        if (i + dx >= H or i + dx < 0 or j + dy >= W or j + dy < 0):
            continue
        if (board[i + dx][j + dy] == '#'):
            return(True)
    return False


for i in range(H):
    for j in range(W):
        if (board[i][j] == '#'):
            if (check(i, j) is False):
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
wall = '.' * (W+2)
s = []
s.append(wall)
for i in range(H):
  tmp = "." + input() + "."
  s.append(tmp)
s.append(wall)
#for i in range(len(s)):
#  print(s[i])
for i in range(1, H+1):
  for j in range(1, W+1):
    #print(s[i][j])
    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 i in range(h+2)]
for i in range(h):
    x=input()
    x="."+x
    x=x+"."
    s[i+1]=list(x)
s[0]=["." for i in range(w+2)]
s[-1]=["." for i in range(w+2)]
f=0
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j]=="#" and s[i][j+1]==s[i][j-1]==s[i+1][j]==s[i-1][j]==".":
            f=1
print("Yes" if f==0 else "No")

Submission status: Accepted

Solution code:
#14 C - Grid Repainting 2
H,W = map(int,input().split())
s = [list(input()) for _ in range(H)]

dw = [0,1,0,-1]
dh = [1,0,-1,0]

#ある座標について
result = 'Yes'
for h in range(H):
    for w in range(W):
        #ある座標の上下左右について
        if s[h][w] == '#':
            cnt = 0   
            for i in range(4):
                nxt_w = w + dw[i]
                nxt_h = h + dh[i]
                if not(-1<nxt_w<W) or not(-1<nxt_h<H):
                    continue
                if s[nxt_h][nxt_w] == '#':
                    cnt += 1
            if cnt == 0:
                result = 'No'
                break            
    else:
        continue
    break

print(result)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
grid = []
for i in range(h):
  grid.append(input())
walk = [[1,0],[-1,0],[0,1],[0,-1]]
ans = 'Yes'
for i in range(h):
  for j in range(w):
    if grid[i][j]=='#':
      temp = 1
      for k,l in walk:
        ni = i+k
        nj = j+l
        if 0<=ni<h and 0<=nj<w:
          if grid[ni][nj]=='#':
            temp = 0
      if temp:
        ans = 'No'
print(ans)

Submission status: Accepted

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

grid = ["*" + input() + "*" for _ in range(h)]
grid.append("*" * (w + 2))
grid.insert(0, "*" * (w+2))

for y in range(1, h+1):
    for x in range(1, w+1):
        if grid[y][x] == "#":
            if grid[y+1][x] == "#" or \
               grid[y-1][x] == "#" or \
               grid[y][x+1] == "#" or \
               grid[y][x-1] == "#":
               continue
            else:
                print("No")
                exit()

print("Yes")

Submission status: Accepted

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

s = [["."] * (W + 2) for i in range(H+2)]

for i in range(1, H + 1):
    ss = input()
    for j in range(1, W + 1):
        s[i][j] = ss[j-1]

#print(s)
f = 0

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-1][j] == "." and s[i][j+1] == "." and s[i][j-1] == ".":
            f =1
    if f == 1:
        break

if f == 0:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
#!/usr/bin/env python

h, w = map(int, input().split())
s = ['' for _ in range(h+2)]
for i in range(1, h+1):
    s[i] = input()
for i in range(w):
    s[0] += '.' 
    s[h+1] += '.' 
for i in range(h+2):
    s[i] = ('.'+s[i]+'.')

for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] == '#':
            if s[i][j-1] == '#' or s[i][j+1] == '#' or s[i-1][j] == '#' or s[i+1][j] == '#' :
                continue
            else:
                print('No')
                exit()
        else:
            continue

print('Yes')

Submission status: Accepted

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

Submission status: Accepted

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

ans = True
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            u_r_d_l = [(-1, 0), (0, 1), (1, 0), (0, -1)]
            if i == 0:
                u_r_d_l[0] = (0, 0)
            if i == h - 1:
                u_r_d_l[2] = (0, 0)
            if j == 0:
                u_r_d_l[3] = (0, 0)
            if j == w - 1:
                u_r_d_l[1] = (0, 0)
            for k in u_r_d_l:
                if k != (0, 0):
                    if s[i + k[0]][j + k[1]] == "#":
                        # ans = True
                        break
            else:
                ans = False


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

Submission status: Accepted

Solution code:
def color(s):
    col = 'black'
    if s == '.':
        col = 'white'
    return col


def main():
    h, w = map(int, input().split())
    s_lst = list(str(input()) for _ in range(h))
    sw_lst = []
    for i in range(h):
        tmp_lst = []
        s = s_lst[i]
        for j in range(w):
            tmp_lst.append(s[j])
        sw_lst.append(tmp_lst)

    flag = True
    black_count = 0

    for i in range(h):
        for j in range(w):

            surroundings = []
            if color(sw_lst[i][j]) == 'black':
                black_count += 1
                if i != 0:
                    surroundings.append(color(sw_lst[i - 1][j]))
                if i != h - 1:
                    surroundings.append(color(sw_lst[i + 1][j]))
                if j != 0:
                    surroundings.append(color(sw_lst[i][j - 1]))
                if j != w - 1:
                    surroundings.append(color(sw_lst[i][j + 1]))

                if 'black' not in surroundings:
                    flag = False

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
h,w=map(int,input().split())
s=[input() for i in range(h)]
ss=[[0 for _ in range(w+2)] for _ in range(h+2)]
for j in range(w):
    for i in range(h):
        ss[i+1][j+1]=s[i][j]
for j in range(1,w+1):
    for i in range(1,h+1):
        c=True
        if ss[i][j]=='.':
            continue
        if not(ss[i-1][j]=='#' or ss[i][j-1]=='#' or ss[i][j+1]=='#' or ss[i+1][j]=='#'):
            print('No')
            sys.exit()
print('Yes')

Submission status: Accepted

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

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

for i in range(H):
    for j in range(W):
        if l[i][j] == '.':
            pass
        elif 0<i and l[i-1][j] == '#':
            pass
        elif 0<j and l[i][j-1] == '#':
            pass
        elif j<W-1 and l[i][j+1] == '#':
            pass
        elif i<H-1 and l[i+1][j] == '#':
            pass
        else:
            print('No')
            exit()
print('Yes')

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    h,w = LI()
    board = [["." for _ in range(w+2)]]
    for _ in range(h):
        line = list(S())
        board.append(["."] + line + ["."])
    board.append(["." for _ in range(w+2)])
    table = [[False]*(w+2) for _ in range(h+2)]
    exp = [[1,0],[0,1],[-1,0],[0,-1]]

    for i in range(h+2):
        for j in range(w+2):
            if board[i][j] == ".":
                table[i][j] = True
            else:
                for x,y in exp:
                    if board[i+x][j+y] == "#":
                        table[i][j] = True
    from functools import reduce
    def kakeru(lst):
        return reduce(lambda x,y: x and y,lst)
    ans = True
    for line in table:
        ans &= kakeru(line)
    print("Yes" if ans else "No")
main()

Submission status: Accepted

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

def judge(i,j,s,H,W):
    find=False
    dh=[0,1,0,-1]
    dw=[1,0,-1,0]
    for n in range(4):
        if 0<=i+dh[n]<=H-1 and 0<=j+dw[n]<=W-1:
            if s[i+dh[n]][j+dw[n]]=="#":
                find=True
                break
    return find

def main():
    end=False
    for i in range(H):
        if end==True:
            break
        for j in range(W):
            if s[i][j]=="#":
                if not judge(i,j,s,H,W):
                    print("No")
                    end=True
                    break
    if end==False:
        print("Yes")
if __name__=="__main__":
	main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

for h in range(1,H-1):
  for w in range(1,W-1):
    if S[h][w]=="#" and S[h-1][w]=="." and S[h+1][w]=="." and S[h][w-1]=="." and S[h][w+1]==".":
      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()))
dx=[1,0,-1,0]
dy=[0,1,0,-1]
for i in range(1,h-1):
    for j in range(1,w-1):
        if S[i][j]=='#':
            if all(S[i+dx[k]][j+dy[k]]=='.' for k in range(4)):
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

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

flag = True

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            sub_flag = False
            if i > 0 and s[i-1][j] == "#":
                sub_flag = True
            if j > 0 and s[i][j-1] == "#":
                sub_flag = True
            if i < h-1 and s[i+1][j] == "#":
                sub_flag = True
            if j < w-1 and s[i][j+1] == "#":
                sub_flag = True

            if sub_flag:
                pass
            else:
                flag = False
                break

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

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
pict = [input() for i in range(H)]
for i in range(H):
    for j in range(W):
        if pict[i][j] == '#':
            flag = False
            for x,y in ((i-1,j),(i+1,j),(i,j-1),(i,j+1)):
                if 0<=x<H and 0<=y<W and pict[x][y]=='#':
                    flag = True
                    break
            if flag == False:
                print('No')
                exit(0)
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
t = [(0, 1), (0, -1), (1, 0), (-1, 0)]
ans = True
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            for h1, w1 in t:
                h2 = h1 + h
                w2 = w1 + w
                if H <= h2 or W <= w2 or h2 < 0 or w2 < 0:
                    continue
                if S[h2][w2] == '#':
                    break
            else:
                ans = False
print('Yes' if ans else 'No')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
s=[input() for _ in range(H)]
S=[]
S.append(['.' for _ in range(W+2)])
for i in range(H):
    s[i]='.'+s[i]+'.'
    S.append(list(s[i]))
S.append(['.' for _ in range(W+2)])
ans=True
for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=='#':
            flag=False
            for k in range(-1,2,2):
                if S[i+k][j]=='#' or S[i][j+k]=='#':
                    flag=True
                    break
            if flag==False:
                ans=False
                break
    else:
        continue
    break
if ans:
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

ss=[['' for j  in range(W+2)]for i in range(H+2)]
for i in range(H):
    for j in range(W):
        ss[i+1][j+1]=s[i][j]

for i in range(H):
    for j in range(W):
        if ss[i][j]=='#':
            if ss[i][j-1]=='#' or ss[i][j+1]=='#' or ss[i-1][j]=='#' or ss[i+1][j]=='#':
                continue
            else:
                print('No')
                exit()
print('Yes')