Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
s = []
for i in range(H):
    s.append(list(input()))

que = deque()
alr = [[float('inf') for i in range(W)] for j in range(H)]
if s[0][0] == '.':
    alr[0][0] = 0
else:
    alr[0][0] = 1
que.append((0,0))
while que:
    h,w = que.popleft()
    if h < H-1:
        que.append((h+1,w))
        if s[h+1][w] == '.':
            alr[h+1][w] = min(alr[h][w],alr[h+1][w])
        else:
            alr[h+1][w] = min(alr[h][w] + 1,alr[h+1][w])
    if w < W-1:
        que.append((h,w+1))
        if s[h][w+1] == '.':
            alr[h][w+1] = min(alr[h][w],alr[h][w+1])
        else:
            alr[h][w+1] = min(alr[h][w] + 1,alr[h][w+1])
print(alr[-1][-1])

Fixed submission:
from collections import deque
H,W = map(int,input().split())
s = []
for i in range(H):
    s.append(list(input()))

que = deque()
alr = [[float('inf') for i in range(W)] for j in range(H)]
if s[0][0] == '.':
    alr[0][0] = 0
else:
    alr[0][0] = 1
que.append((0,0))
while que:
    h,w = que.popleft()
    if s[h][w] == '#':
        flag = True
    else:
        flag = False

    if h < H-1:
        if s[h+1][w] == '.':
            if alr[h+1][w] > alr[h][w]:
                alr[h+1][w] = alr[h][w]
                que.append((h+1,w))
        else:
            if flag:
                if alr[h+1][w] > alr[h][w]:
                    alr[h+1][w] = alr[h][w]
                    que.append((h+1,w))
            else:
                if alr[h+1][w] > alr[h][w]+1:
                    alr[h+1][w] =alr[h][w]+1
                    que.append((h+1,w))            
    if w < W-1:
        if s[h][w+1] == '.':
            if alr[h][w+1] > alr[h][w]:
                alr[h][w+1] =alr[h][w]
                que.append((h,w+1))
        else:
            if flag:
                if alr[h][w+1] > alr[h][w]:
                    alr[h][w+1] =alr[h][w]
                    que.append((h,w+1))
            else:
                if alr[h][w+1] > alr[h][w]+1:
                    que.append((h,w+1))
                    alr[h][w+1] = alr[h][w] + 1
print(alr[-1][-1])

Buggy python submission (verdict: Runtime Error):
5 5
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.

Fixed submission:
from collections import deque
H,W = map(int,input().split())
s = []
for i in range(H):
    s.append(list(input()))

que = deque()
alr = [[float('inf') for i in range(W)] for j in range(H)]
if s[0][0] == '.':
    alr[0][0] = 0
else:
    alr[0][0] = 1
que.append((0,0))
while que:
    h,w = que.popleft()
    if s[h][w] == '#':
        flag = True
    else:
        flag = False

    if h < H-1:
        if s[h+1][w] == '.':
            if alr[h+1][w] > alr[h][w]:
                alr[h+1][w] = alr[h][w]
                que.append((h+1,w))
        else:
            if flag:
                if alr[h+1][w] > alr[h][w]:
                    alr[h+1][w] = alr[h][w]
                    que.append((h+1,w))
            else:
                if alr[h+1][w] > alr[h][w]+1:
                    alr[h+1][w] =alr[h][w]+1
                    que.append((h+1,w))            
    if w < W-1:
        if s[h][w+1] == '.':
            if alr[h][w+1] > alr[h][w]:
                alr[h][w+1] =alr[h][w]
                que.append((h,w+1))
        else:
            if flag:
                if alr[h][w+1] > alr[h][w]:
                    alr[h][w+1] =alr[h][w]
                    que.append((h,w+1))
            else:
                if alr[h][w+1] > alr[h][w]+1:
                    que.append((h,w+1))
                    alr[h][w+1] = alr[h][w] + 1
print(alr[-1][-1])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
s = []
for i in range(H):
    s.append(list(input()))

que = deque()
alr = [[float('inf') for i in range(W)] for j in range(H)]
if s[0][0] == '.':
    alr[0][0] = 0
else:
    alr[0][0] = 1
que.append((0,0))
while que:
    h,w = que.popleft()
    if h < H-1:
        que.append((h+1,w))
        if s[h+1][w] == '.':
            alr[h+1][w] = min(alr[h][w],alr[h+1][w])
        else:
            alr[h+1][w] = min(alr[h][w] + 1,alr[h+1][w])
    if w < W-1:
        que.append((h,w+1))
        if s[h][w+1] == '.':
            alr[h][w+1] = min(alr[h][w],alr[h][w+1])
        else:
            alr[h][w+1] = min(alr[h][w] + 1,alr[h][w+1])
print(alr[-1][-1])

Fixed submission:
from collections import deque
H,W = map(int,input().split())
s = []
for i in range(H):
    s.append(list(input()))

que = deque()
alr = [[float('inf') for i in range(W)] for j in range(H)]
if s[0][0] == '.':
    alr[0][0] = 0
else:
    alr[0][0] = 1
que.append((0,0))
while que:
    h,w = que.popleft()
    if s[h][w] == '#':
        flag = True
    else:
        flag = False

    if h < H-1:
        if s[h+1][w] == '.':
            if alr[h+1][w] > alr[h][w]:
                alr[h+1][w] = alr[h][w]
                que.append((h+1,w))
        else:
            if flag:
                if alr[h+1][w] > alr[h][w]:
                    alr[h+1][w] = alr[h][w]
                    que.append((h+1,w))
            else:
                if alr[h+1][w] > alr[h][w]+1:
                    alr[h+1][w] =alr[h][w]+1
                    que.append((h+1,w))            
    if w < W-1:
        if s[h][w+1] == '.':
            if alr[h][w+1] > alr[h][w]:
                alr[h][w+1] =alr[h][w]
                que.append((h,w+1))
        else:
            if flag:
                if alr[h][w+1] > alr[h][w]:
                    alr[h][w+1] =alr[h][w]
                    que.append((h,w+1))
            else:
                if alr[h][w+1] > alr[h][w]+1:
                    que.append((h,w+1))
                    alr[h][w+1] = alr[h][w] + 1
print(alr[-1][-1])