Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h,w = map(int,input().split())
c = []
c.append(['×' for _ in range(w+2)])
for _ in range(h):
	c.append(list('×'+input()+'×'))
c.append(['×' for _ in range(w+2)])
ans = [[101 for _ in range(w+2)]for _ in range(h+2)]
if c[1][1] == '.':
	ans[1][1] = 0
else:
	ans[1][1] = 1

q = deque()

def bfs(x,y):
	global q
	global c
	q.append([x,y])

	while q:
		x,y = q.popleft()

		if [x,y] == [w,h]:
			return ans[h][w]
		if c[y][x+1] == '.':
			q.append([x+1,y])
			ans[y][x+1] = min(ans[y][x+1],ans[y][x])
		elif c[y][x+1] == '#':
			q.append([x+1,y])
			ans[y][x+1] = min(ans[y][x+1],ans[y][x]+1)
		if c[y+1][x] == '.':
			q.append([x,y+1])
			ans[y+1][x] = min(ans[y+1][x],ans[y][x])
		elif c[y+1][x] == '#':
			q.append([x,y+1])
			ans[y+1][x] = min(ans[y+1][x],ans[y][x]+1)


print(bfs(1,1))


Fixed submission:
h,w = map(int,input().split())

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

inf = float('inf')
dp = [[inf for _ in range(w)] for _ in range(h)]
dp[0][0] = 0

for i in range(h):
	for j in range(w):
		if s[i][j] == ".":
			if j > 0 and i > 0:
				dp[i][j] = min(dp[i-1][j],dp[i][j-1])
			elif j > 0 and i == 0:
				dp[i][j] = dp[i][j-1]
			elif j == 0 and i > 0:
				dp[i][j] = dp[i-1][j]
		else:
			tmp1 = inf
			tmp2 = inf
			if i > 0:
				tmp1 = dp[i-1][j]
				if s[i-1][j] == ".":
					tmp1 += 1
			if j > 0:
				tmp2 = dp[i][j-1]
				if s[i][j-1] == ".":
					tmp2 += 1
			dp[i][j] = min(tmp1,tmp2)
			if i==0 and j==0:
				dp[i][j] = 1

print(dp[h-1][w-1])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h,w = map(int,input().split())
c = []
c.append(['×' for _ in range(w+2)])
for _ in range(h):
	c.append(list('×'+input()+'×'))
c.append(['×' for _ in range(w+2)])
ans = [[101 for _ in range(w+2)]for _ in range(h+2)]
if c[1][1] == '.':
	ans[1][1] = 0
else:
	ans[1][1] = 1

q = deque()

def bfs(x,y):
	global q
	global c
	q.append([x,y])

	while q:
		x,y = q.popleft()

		if [x,y] == [w,h]:
			return ans[h][w]
		if c[y][x+1] == '.':
			q.append([x+1,y])
			#c[y][x+1] = '×'
			if ans[y][x+1] == 101:
				ans[y][x+1] = ans[y][x]
			else:
				ans[y][x+1] = min(ans[y][x+1],ans[y][x])
		elif c[y][x+1] == '#':
			q.append([x+1,y])
			#c[y][x+1] = '×'
			if ans[y][x+1] == 101:
				ans[y][x+1] = ans[y][x] + 1
			else:
				ans[y][x+1] = min(ans[y][x+1],ans[y][x]+1)
		if c[y+1][x] == '.':
			q.append([x,y+1])
			#c[y+1][x] = '×'
			if ans[y+1][x] == 101:
				ans[y+1][x] = ans[y][x]
			else:
				ans[y+1][x] = min(ans[y+1][x],ans[y][x])
		elif c[y+1][x] == '#':
			q.append([x,y+1])
			#c[y+1][x] = '×'
			if ans[y+1][x] == 101:
				ans[y+1][x] = ans[y][x] + 1
			else:
				ans[y+1][x] = min(ans[y+1][x],ans[y][x]+1)


print(bfs(1,1))


Fixed submission:
h,w = map(int,input().split())

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

inf = float('inf')
dp = [[inf for _ in range(w)] for _ in range(h)]
dp[0][0] = 0

for i in range(h):
	for j in range(w):
		if s[i][j] == ".":
			if j > 0 and i > 0:
				dp[i][j] = min(dp[i-1][j],dp[i][j-1])
			elif j > 0 and i == 0:
				dp[i][j] = dp[i][j-1]
			elif j == 0 and i > 0:
				dp[i][j] = dp[i-1][j]
		else:
			tmp1 = inf
			tmp2 = inf
			if i > 0:
				tmp1 = dp[i-1][j]
				if s[i-1][j] == ".":
					tmp1 += 1
			if j > 0:
				tmp2 = dp[i][j-1]
				if s[i][j-1] == ".":
					tmp2 += 1
			dp[i][j] = min(tmp1,tmp2)
			if i==0 and j==0:
				dp[i][j] = 1

print(dp[h-1][w-1])