from itertools import product

from collections import deque

import numpy as np

inputs = open(0).readlines()

class Grid():

    def __init__(self, grid, w=0, h=0, function=lambda x: x):

        self.w = w = w if w else len(grid[0])

        self.h = h = h if h else len(grid)

        dtype = type(function(grid[0][0]))

        self.grid = np.empty((h, w), dtype=dtype)

        for i, row in zip(list(range(h)), grid):

            for j, val in zip(list(range(w)), row):

                self.grid[i][j] = function(val)

    

    def is_valid_x(self, x):

        return 0 <= x < self.w

    def is_valid_y(self, y):

        return 0 <= y < self.h

    def is_valid_xy(self, x, y):

        return self.is_valid_x(x) and self.is_valid_y(y) 

    

    def __iter__(self):

        return iter(self.grid)

    def __repr__(self):

        return '\n'.join([' '.join(map(str, row)) for row in self.grid])

    def __getitem__(self, x):

        return self.grid[x]

    def __setitem__(self, x, val):

        self.grid[x] = val



h, w = list(map(int, inputs[0].split()))

grid = Grid(inputs[1:], function=lambda x: int(x == '#'))



def dfs(root):

    count = 1

    x, y = root

    grid[y, x] = 0

    stack = [root]

    while stack:

        x, y = stack.pop()

        grid[y, x] = 0

        for dx, dy in zip([1, 0, -1, 0], [0, 1, 0, -1]):

            if grid.is_valid_xy(x+dx, y+dy) and grid[y+dy, x+dx]:

                stack.append((x+dx, y+dy))

                count += 1

    return count

    

ans = 'Yes'

for i, j in product(list(range(h)), list(range(w))):

    if grid[i, j]:

        if dfs((j, i)) == 1:

            ans = 'No'

            break

print(ans)
from itertools import product

from collections import deque

import numpy as np

class Grid:

    def __init__(self, grid, w=0, h=0, function=lambda x: x):

        self.w = w = w if w else len(grid[0])

        self.h = h = h if h else len(grid)

        dtype = type(function(grid[0][0]))

        self.grid = np.empty((h, w), dtype=dtype)

        for i, row in zip(list(range(h)), grid):

            for j, val in zip(list(range(w)), row):

                self.grid[i][j] = function(val)

    

    def is_valid_x(self, x):

        return 0 <= x < self.w

    def is_valid_y(self, y):

        return 0 <= y < self.h

    def is_valid_xy(self, x, y):

        return self.is_valid_x(x) and self.is_valid_y(y) 

    

    def __iter__(self):

        return iter(self.grid)

    def __repr__(self):

        return '\n'.join([' '.join(map(str, row)) for row in self.grid]) + '\n'

    def __getitem__(self, x):

        return self.grid[x]

    def __setitem__(self, x, val):

        self.grid[x] = val





def dfs(grid, root):

    stack = [root]

    x, y = root

    grid[y, x] = '!'

    while stack:

        x, y = stack.pop()

        yield (x, y)

        for dx, dy in zip([1, 0, -1, 0], [0, 1, 0, -1]):

            nx, ny = x+dx, y+dy

            if grid.is_valid_xy(nx, ny) and grid[ny, nx] == '#':

                stack.append((nx, ny))

                grid[ny, nx] = '!'



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

grid = Grid([eval(input()) for s in range(h)])



for y, x in product(list(range(h)), list(range(w))):

    if grid[y, x] == '#':

        if len(list(dfs(grid, (x, y)))) == 1:

            print('No')

            break

else:

    print('Yes')
from itertools import product
from collections import deque
import numpy as np

inputs = open(0).readlines()


class Grid:
    def __init__(self, grid, w=0, h=0, function=lambda x: x):
        self.w = w = w if w else len(grid[0])
        self.h = h = h if h else len(grid)
        dtype = type(function(grid[0][0]))
        self.grid = np.empty((h, w), dtype=dtype)
        for i, row in zip(list(range(h)), grid):
            for j, val in zip(list(range(w)), row):
                self.grid[i][j] = function(val)

    def is_valid_x(self, x):
        return 0 <= x < self.w

    def is_valid_y(self, y):
        return 0 <= y < self.h

    def is_valid_xy(self, x, y):
        return self.is_valid_x(x) and self.is_valid_y(y)

    def __iter__(self):
        return iter(self.grid)

    def __repr__(self):
        return "\n".join([" ".join(map(str, row)) for row in self.grid])

    def __getitem__(self, x):
        return self.grid[x]

    def __setitem__(self, x, val):
        self.grid[x] = val


h, w = list(map(int, inputs[0].split()))
grid = Grid(inputs[1:], function=lambda x: int(x == "#"))


def dfs(root):
    count = 1
    x, y = root
    grid[y, x] = 0
    stack = [root]
    while stack:
        x, y = stack.pop()
        grid[y, x] = 0
        for dx, dy in zip([1, 0, -1, 0], [0, 1, 0, -1]):
            if grid.is_valid_xy(x + dx, y + dy) and grid[y + dy, x + dx]:
                stack.append((x + dx, y + dy))
                count += 1
    return count


ans = "Yes"
for i, j in product(list(range(h)), list(range(w))):
    if grid[i, j]:
        if dfs((j, i)) == 1:
            ans = "No"
            break
print(ans)

from itertools import product
from collections import deque
import numpy as np


class Grid:
    def __init__(self, grid, w=0, h=0, function=lambda x: x):
        self.w = w = w if w else len(grid[0])
        self.h = h = h if h else len(grid)
        dtype = type(function(grid[0][0]))
        self.grid = np.empty((h, w), dtype=dtype)
        for i, row in zip(list(range(h)), grid):
            for j, val in zip(list(range(w)), row):
                self.grid[i][j] = function(val)

    def is_valid_x(self, x):
        return 0 <= x < self.w

    def is_valid_y(self, y):
        return 0 <= y < self.h

    def is_valid_xy(self, x, y):
        return self.is_valid_x(x) and self.is_valid_y(y)

    def __iter__(self):
        return iter(self.grid)

    def __repr__(self):
        return "\n".join([" ".join(map(str, row)) for row in self.grid]) + "\n"

    def __getitem__(self, x):
        return self.grid[x]

    def __setitem__(self, x, val):
        self.grid[x] = val


def dfs(grid, root):
    stack = [root]
    x, y = root
    grid[y, x] = "!"
    while stack:
        x, y = stack.pop()
        yield (x, y)
        for dx, dy in zip([1, 0, -1, 0], [0, 1, 0, -1]):
            nx, ny = x + dx, y + dy
            if grid.is_valid_xy(nx, ny) and grid[ny, nx] == "#":
                stack.append((nx, ny))
                grid[ny, nx] = "!"


h, w = list(map(int, input().split()))
grid = Grid([eval(input()) for s in range(h)])
for y, x in product(list(range(h)), list(range(w))):
    if grid[y, x] == "#":
        if len(list(dfs(grid, (x, y)))) == 1:
            print("No")
            break
else:
    print("Yes")

- -inputs = open(0).readlines() -        return "\n".join([" ".join(map(str, row)) for row in self.grid]) +        return "\n".join([" ".join(map(str, row)) for row in self.grid]) + "\n" -h, w = list(map(int, inputs[0].split())) -grid = Grid(inputs[1:], function=lambda x: int(x == "#")) +def dfs(grid, root): +    stack = [root] +    x, y = root +    grid[y, x] = "!" +    while stack: +        x, y = stack.pop() +        yield (x, y) +        for dx, dy in zip([1, 0, -1, 0], [0, 1, 0, -1]): +            nx, ny = x + dx, y + dy +            if grid.is_valid_xy(nx, ny) and grid[ny, nx] == "#": +                stack.append((nx, ny)) +                grid[ny, nx] = "!" -def dfs(root): -    count = 1 -    x, y = root -    grid[y, x] = 0 -    stack = [root] -    while stack: -        x, y = stack.pop() -        grid[y, x] = 0 -        for dx, dy in zip([1, 0, -1, 0], [0, 1, 0, -1]): -            if grid.is_valid_xy(x + dx, y + dy) and grid[y + dy, x + dx]: -                stack.append((x + dx, y + dy)) -                count += 1 -    return count - - -ans = "Yes" -for i, j in product(list(range(h)), list(range(w))): -    if grid[i, j]: -        if dfs((j, i)) == 1: -            ans = "No" +h, w = list(map(int, input().split())) +grid = Grid([eval(input()) for s in range(h)]) +for y, x in product(list(range(h)), list(range(w))): +    if grid[y, x] == "#": +        if len(list(dfs(grid, (x, y)))) == 1: +            print("No") -print(ans) +else: +    print("Yes")