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: Runtime Error

Solution code:
h,w = map(int,input().split())
s = [input() for _ in range(w)]
l = []
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] == "#":
                l.append(0)
            else:
                l.append(1)

if sum(l) == 0:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

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

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

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: Runtime Error

Solution code:
import math
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

# grid
# N = I()
# 黒く塗りつぶす#が独立していたら不可
H,W = LI()
S = [list(_S()) for _ in range (H)]

def check(x, y):
    # 移動する4方向をループ
    res = False
    # for dx,dy in itertools.product([-1,0,1],repeat=2):
    dxs = [-1,1]
    dys = [-1,1]
    for dx in dxs:
        nx = x + dx
        ny = y
        if 0 <= nx and nx < H and S[nx][ny] == "#":
            res = True
    for dy in dys:
        nx = x
        ny = y + dy
        if 0 <= ny and ny < H and S[nx][ny] == "#":
            res = True
    return res

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            if not check(i,j):
                print('No')
                exit()

print('Yes')

Submission status: Runtime Error

Solution code:
ans = True
d = [(0,1),(0,-1),(1,0),(-1,0)]
for y in range(H):
    for x in range(W):
        if s[y][x] == "#":
            for dy, dx in d:
                ny = y + dy
                nx = x + dx
                if ny<0 or H<=ny or nx<0 or W<=nx:
                    continue
                if s[ny][nx]=="#":
                    break
            else:
                ans = False
print("Yes" if ans else "No")

Submission status: Runtime Error

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

d = [-1, 1]
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            ls = []
            for i in range(2):
                for j in range(2):
                    hs = h + d[i]
                    ws = w + d[j]
                    if 0 <= hs < H and 0 <= ws < W:
                        ls.append(S[hs][w])
                        ls.append(S[h][ws])

if '#' in ls:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

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

for h in range(1, H + 1):
    for w in range(1, W + 1):
        if s[h][w] == '#':
            if 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: Runtime Error

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

for h in range(1, H + 1):
    for w in range(1, W + 1):
        if s[h][w] == '#':
            if 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: Runtime Error

Solution code:
H, W = map(int, input().split())
S = ["."*(W+2)] + ["." + input() + "." for _ in range(H)] + ["."*(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] + S[y+1][x] + S[y][x-1] + S[y][x+1]).count('#') == 0:
            print("No")
            exit()
print("Yes")

Submission status: Runtime Error

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

f = False
for h in range(H):
  pre = '.'
  now = S[h][0]
  nxt = S[h][1]
  abv = '.' if h == 0 else S[h-1][0]
  blw = '.' if h == H-1 else S[h+1][0]
  for w in range(W):
    if now == '.':
      f = False
    elif now in [pre, nxt, abv, blw]:
      f = False
    else:
      f = True
    
    pre = now
    now = nxt
    nxt = '.' if w == W-1 else S[h][w+1]
    abv = '.' if h == 0 else S[h-1][w]
    blw = '.' if h == H-1 else S[h+1][w]
    
    if f:
      print('No')
      exit()
print('Yes')

Submission status: Runtime Error

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

xy=((-1,0),(-1,0),(1,0),(0,1))
S=[]
S.append("."*(W+2))
for i in range(H):
    S.append("."+input()+".")

S.append("."*(W+2))

for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]==".":
            continue
        ans=False
        for x,y in xy:
            if S[i+x][j+y]=="#":
                ans=True
                break

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

Submission status: Runtime Error

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

padded = [[0 for i in range(W+2)] for j in range(H+2)]

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

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

Submission status: Runtime Error

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

for i in range(H):
    for j in range(W):
        if arr[i][j] == '#':
            flg =False
            if i-1>=0:
                if arr[i-1][j] == '#':
                    flg = True
            if j-1>=0:
                if arr[i][j-1] == '#':
                    flg = True
            if i+1<W:
                if arr[i+1][j] == '#':
                    flg = True
            if j+1<H:
                if arr[i][j+1] == '#':
                    flg = True
            if flg == False:
                print('No')
                exit()

print('Yes')

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
s=[list(input())for i in range(H)]
c=[['.']*W for i in range(H)]
for i in range(H):
    countX=0
    nowX=s[i][0]
    countY=0
    nowY=s[0][i]
    for j in range(W):
        if nowX==s[i][j]:
            countX+=1
            if 1<countX and nowX=='#':
                c[i][j-1],c[i][j]='#','#'
        else:
            countX=1
        if nowY==s[j][i]:
            countY+=1
            if 1<countY and nowY =='#':
                c[j-1][i],c[j][i]='#','#'
        else:
            countY=1
        nowX=s[i][j]
        nowY=s[j][i]
print('Yes' if c==s else 'No')

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
field = [list(input()) for _ in range(h)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
count = 0
for i in range(h):
    for j in range(w):
        if field[i][j] == '#':
            count = 0
            for d in range(4):
                if 0<=i+dx[d]<w and 0<=j+dy[d]<h:
                    count += 1 if field[i+dx[d]][j+dy[d]]=='#' else 0

            if count<1:
                print('No')
                exit()
print('Yes')

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split(" "))
s = []
for i in range(h):
    s.append(str(input()))
d = [(1, 0), (0, 1), (-1, 0), (0, -1)]
ss = ["" for i in range(h)]
for i in range(h):
    for j in range(w):
        f = False
        if s[j][i] == ".":
            f = True
            continue
        for dx, dy in d:
            if 0 <= i + dx < w and 0 <= j + dy < h:
                if s[j + dy][i + dx] == "#":
                    f = True
                    break
        if not f:
            break
    if not f:
        break

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
h,w = map(int, input().split())
board = {}
board[-1] = ["-"] * h
board[h] = ["-"] * h
for hi in range(h):
  board[hi] = {}
  board[hi][-1] = ["-"]
  board[hi][w] = ["-"]
  row = list(input())
  for wi in range(w):
    board[hi][wi] = row[wi]

res="Yes"
for hi in range(h):
  if res == "No":
    break
  for wi in range(w):
    if board[hi][wi] == "#" and board[hi-1][wi] != "#" and board[hi][wi-1] != "#" and board[hi+1][wi] != "#" and board[hi][wi+1] != "#":
        res = "No"
        break

print(res)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
h,w = map(int, input().split())
board = {}
board[-1] = ["-"] * h
board[h] = ["-"] * h
for hi in range(h):
  board[hi] = {}
  board[hi][-1] = ["-"] * w
  board[hi][w] = ["-"] * w
  row = list(input())
  for wi in range(w):
    board[hi][wi] = row[wi]

res="Yes"
for hi in range(h):
  if res == "No":
    break
  for wi in range(w):
    if board[hi][wi] == "#" and board[hi-1][wi] != "#" and board[hi][wi-1] != "#" and board[hi+1][wi] != "#" and board[hi][wi+1] != "#":
        res = "No"
        break

print(res)

Submission status: Runtime Error

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

for h in range(H):
    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: Runtime Error

Solution code:
def dfs(f, stack, cnt):
    while stack:
        y, x = stack.pop()
        f[y][x] = '.'
        cnt += 1

        dxy = [(0,1), (0,-1), (1,0), (-1,0)]
        for dx, dy in dxy:
            nx, ny = x + dx, y + dy
            if 0 <= nx < w and 0 <= ny < h and f[ny][nx] == '#':
                stack.append([ny, nx])
                cnt, f = dfs(f, stack, cnt)
    return cnt, f

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

flg = False
dxy = [(0,1), (0,-1), (1,0), (-1,0)]
stack = []
for y in range(h):
    for x in range(w):
        if f[y][x] == '#':
            stack.append([y, x])
            cnt = 0
            cnt, f = dfs(f, stack, cnt)
            if cnt < 2:
                flg = True

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

Submission status: Runtime Error

Solution code:
def dfs(f, stack, cnt):
    while stack:
        y, x = stack.pop()
        f[y][x] = '.'
        cnt += 1

        dxy = [(0,1), (0,-1), (1,0), (-1,0)]
        for dx, dy in dxy:
            nx, ny = x + dx, y + dy
            if 0 <= nx < w and 0 <= ny < h and f[ny][nx] == '#':
                stack.append([ny, nx])
                cnt, f = dfs(f, stack, cnt)
    return cnt, f

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

flg = False
dxy = [(0,1), (0,-1), (1,0), (-1,0)]
stack = []
for y in range(h):
    for x in range(w):
        if f[y][x] == '#':
            stack.append([y, x])
            cnt = 0
            cnt, f = dfs(f, stack, cnt)
            if cnt < 2:
                flg = True
                break
    if flg:
        break

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

Submission status: Runtime Error

Solution code:
def dfs(f, stack, cnt):
    while stack:
        x, y = stack.pop()
        f[y][x] = '.'
        cnt += 1

        dxy = [(0,1), (0,-1), (1,0), (-1,0)]
        for dx, dy in dxy:
            nx, ny = x + dx, y + dy
            if 0 <= nx < w and 0 <= ny < h:
                if f[ny][nx] == '#':
                    stack.append([nx, ny])
                    cnt = dfs(f, stack, cnt)
    return cnt


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

flg = False
dxy = [(0,1), (0,-1), (1,0), (-1,0)]
stack = []
for y in range(h):
    for x in range(w):
        if f[y][x] == '#':
            stack.append([x, y])
            cnt = 0
            cnt = dfs(f, stack, cnt)
            if cnt < 2:
                flg = True
                break
    if flg:
        break

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

Submission status: Runtime Error

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(w)
    
    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: Runtime Error

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(w)
    
    flag = False

    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
            flag = True
            break
        if flag:
            print('No')
            exit()

    print('Yes')

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

if H == 1 and W ==1:
	B = str(input())
	if B == '#':
		print('No')
		exit()
	else:
		print('Yes')
		exit()
elif H == 1:
	A = str(input())
	if A[0] == '#':
		if A[1] == '.':
			print('No')
			exit()
	for i in range (1, W):
		if A[i] == '#' and A[i-1] == '.':
			print('No')
			exit()
	print('Yes')
	exit()
elif W == 1:
	for i in range (0, H):
		A.append(input())
	if A[0] == '#':
		if A[1] == '.':
			print('No')
			exit()
	for i in range (1, H):
		if A[i] == '#' and A[i-1] == '.':
			print('No')
			exit()
	print('Yes')
	exit()


for i in range (0, H):
	A.append(str(input()))
    
counter = 0

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

Submission status: Runtime Error

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

for i in range (0, H):
	A.append(str(input()))
    
counter = 0

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

Submission status: Runtime Error

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 [s_list() 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(w)
    
    flag = False

    for i in range(h):
        for j in range(w):
            if str_list[i][0][j] == '.':
                continue
            if i - 1 >= 0:
                if str_list[i-1][0][j] == '#':
                    continue
            if j - 1 >= 0:
                if str_list[i][0][j-1] == '#':
                    continue
            if i + 1 < w:
                if str_list[i+1][0][j] == '#':
                    continue
            if j + 1 < h:
                if str_list[i][0][j+1] == '#':
                    continue
            flag = True
            break
        if flag:
            print('No')
            exit()

    print('Yes')

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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 [s_list() 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(w)
    
    flag = False

    for i in range(h):
        for j in range(w):
            if str_list[i][0][j] == '.':
                continue
            if i - 1 >= 0:
                if str_list[i-1][0][j] == '#':
                    continue
            if j - 1 >= 0:
                if str_list[i][0][j-1] == '#':
                    continue
            if i + 1 < h:
                if str_list[i+1][0][j] == '#':
                    continue
            if j + 1 < w:
                if str_list[i][0][j+1] == '#':
                    continue
            flag = True
            break
        if flag:
            print('No')
            exit()

    print('Yes')

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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 [s_list() 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(w)
    
    flag = False

    for i in range(h):
        for j in range(w):
            if str_list[i][0][j] == '.':
                continue
            if i - 1 >= 0 and str_list[i-1][0][j] == '#':
                continue
            if j - 1 >= 0 and str_list[i][0][j-1] == '#':
                continue
            if i + 1 < h and str_list[i+1][0][j] == '#':
                continue
            if j + 1 < w and str_list[i][0][j+1] == '#':
                continue
            flag = True
            break
        if flag:
            print('No')
            exit()

    print('Yes')

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
V,W = map(int,input().split())
canvas = []
for i in range(V):
  canvas.append(list(map(str,input())))

  
def line():
  for i in range(V):
    for s in range(W-1):
      if canvas[i][s]=="#" and canvas[i][s+1]=="#":
        return True
        break
      break
  
              
def row():
  for i in range(V-1):
    for s in range(W):
      if canvas[s][i]=="#" and canvas[s][i+1]=="#":
        return True
        break
      break
     
        
line = line()
row = row()
if line != True and row != True:
  print("No")
else:
  print("Yes")

Submission status: Runtime Error

Solution code:
V,W = map(int,input().split())
canvas = []
for i in range(V):
  canvas.append(list(map(str,input())))

line = []
for i in range(V):
  for s in range(W-1):
    if canvas[i][s]=="#" and canvas[i][s+1]=="#":
      line.append("OK")
    else:
      line.append("NO")
        
row = []        

for i in range(V-1):
  for s in range(W):
    if canvas[s][i]=="#" and canvas[s][i+1]=="#":
      row.append("OK")
    else:
      row.append("NO")
      
if "OK" not in line and "OK" not in row:
  print("No")
else:
  print("Yes")

Submission status: Runtime Error

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


class UnionFind():
    """
    https://note.nkmk.me/python-union-find/
    DFSの上位互換と考えて良い
    ２要素x, yがpath-connectedかどうかをlogオーダーで判定する（螺旋本の14.1節参照）
    さらに連結成分の要素数がO(1)で取得可能なように改造してある
    """
    def __init__(self, n):
        """
        要素数をnとして、各ノードを0,1,...,(n-1)の番号で管理する
        parentsは各ノードの属する木の根を表す
        ただし根ノードのparentには(その木のノード数)*(-1)を格納する
        """
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        """
        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, yのそれぞれ属する木Tx, Tyの根同士を繋ぐ
        このとき木の要素数が小さい方を大きい方に繋ぐ（rankではなくsizeを用いる）
        """
        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):
        """
        xの属する木の要素数を返す
        根の親を要素数の(-1)倍で定めておいたおかげでO(1)で取得可能
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """
        xとyがpath-connectedかを判定する
        """
        return self.find(x) == self.find(y)

    def members(self, x):
        """
        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())
    S = [input() for _ in range(H)]
    tree = UnionFind(H * W)
    for i in range(H):
        for j in range(W):
            if S[i][j] != "#": continue
            for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                ni, nj = i + di, j + dj
                if 0 <= ni < H and 0 <= nj < W and S[ni][nj] == "#":
                    tree.union(H * i + j, H * ni + nj)

    accomp = True
    for root in tree.roots():
        i, j = root // W, root % W
        if S[i][j] == "#" and tree.size(root) == 1:
            accomp = False
            #break
    if accomp: print("Yes")
    else: print("No")

if __name__ == "__main__":
    main()