Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
H, W = [int(x) for x in input().strip().split()]
G = [0 for _ in range(H)]
C = [[float('inf') for _ in range(W)] for _ in range(H)]

pre = ''
NH = H
for h in range(H):
    s = list(input().strip())
    if s != pre:
        G[h] = s
        pre = s
    else:
        NH -= 1

H = NH
def dfs(r, c, cost):
    # print(' -- dfs --')
    C[r][c] = cost
    ret = deque([])
    q = deque([(r, c)])
    while q:
        cr, cc = q.popleft()
        # print('    cr = {}, cc = {}'.format(cr, cc))
        for i, j in [(0, 1), (1, 0)]:
            if not(0 <= cr + i < H and 0 <= cc + j < W):
                continue
            if C[cr+i][cc+j] != float('inf'):
                continue
            if G[r][c] == G[cr+i][cc+j]:
                C[cr+i][cc+j] = cost
                q.append((cr+i, cc+j))
            else:
                ret.append((cr+i, cc+j))
    # print(ret)
    return ret

q = deque([(0, 0)])
cost = 0
while True:
    # print('q = {}'.format(q))
    nq = deque([])
    if G[q[0][0]][q[0][1]] == '#':
        cost += 1
    for r, c in q:
        # print('  r = {}, c = {}'.format(r, c))
        nq.extend(dfs(r, c, cost))
    q = nq
    # print(C)
    # print('nq = {}'.format(nq))
    if C[H-1][W-1] != float('inf'):
        print(C[H-1][W-1])
        exit()

Fixed submission:
import sys
import numpy as np
from collections import deque
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
H, W = [int(x) for x in input().strip().split()]
G = np.zeros((H, W), dtype=int)
pre = np.ones(W, dtype=int) * -1
NH = H
for h in range(H):
    s = np.array([1 if s == '#' else 0 for s in input().strip()])
    if np.all(s == pre):
        NH -= 1
    else:
        G[h, :] = s
        pre = s

H = NH
C = [[float('inf') for _ in range(W)] for _ in range(H)]
pre = ''
def dfs(r, c, cost):
    # print(' -- dfs --')
    C[r][c] = cost
    ret = deque([])
    q = deque([(r, c)])
    while q:
        cr, cc = q.popleft()
        # print('    cr = {}, cc = {}'.format(cr, cc))
        for i, j in [(0, 1), (1, 0)]:
            if not(0 <= cr + i < H and 0 <= cc + j < W):
                continue
            if C[cr+i][cc+j] != float('inf'):
                continue
            if G[r][c] == G[cr+i][cc+j]:
                C[cr+i][cc+j] = cost
                q.append((cr+i, cc+j))
                if cr+i == H-1 and cc+j == W-1:
                    return ret
            else:
                ret.append((cr+i, cc+j))
    # print(ret)
    return ret
q = deque([(0, 0)])
cost = 0
while True:
    # print('q = {}'.format(q))
    nq = deque([])
    if G[q[0][0]][q[0][1]] == 1:
        cost += 1
    for r, c in q:
        # print('  r = {}, c = {}'.format(r, c))
        if C[r][c] != float('inf'):
            continue
        nq.extend(dfs(r, c, cost))
    q = nq
    # print(C)
    # print('nq = {}'.format(nq))
    if C[H-1][W-1] != float('inf'):
        print(C[H-1][W-1])
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
H, W = [int(x) for x in input().strip().split()]
G = [0 for _ in range(H)]
C = [[float('inf') for _ in range(W)] for _ in range(H)]

pre = ''
NH = H
for h in range(H):
    s = list(input().strip())
    if s != pre:
        G[h] = s
        pre = s
    else:
        NH -= 1

H = NH
def dfs(r, c, cost):
    # print(' -- dfs --')
    C[r][c] = cost
    ret = deque([])
    q = deque([(r, c)])
    while q:
        cr, cc = q.popleft()
        # print('    cr = {}, cc = {}'.format(cr, cc))
        for i, j in [(0, 1), (1, 0)]:
            if not(0 <= cr + i < H and 0 <= cc + j < W):
                continue
            if C[cr+i][cc+j] != float('inf'):
                continue
            if G[r][c] == G[cr+i][cc+j]:
                C[cr+i][cc+j] = cost
                q.append((cr+i, cc+j))
            else:
                ret.append((cr+i, cc+j))
    # print(ret)
    return ret

q = deque([(0, 0)])
cost = 0
while True:
    # print('q = {}'.format(q))
    nq = deque([])
    if G[q[0][0]][q[0][1]] == '#':
        cost += 1
    for r, c in q:
        # print('  r = {}, c = {}'.format(r, c))
        nq.extend(dfs(r, c, cost))
    q = nq
    # print(C)
    # print('nq = {}'.format(nq))
    if C[H-1][W-1] != float('inf'):
        print(C[H-1][W-1])
        exit()

Fixed submission:
import sys
import numpy as np
from collections import deque
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
H, W = [int(x) for x in input().strip().split()]
G = np.zeros((H, W), dtype=int)
pre = np.ones(W, dtype=int) * -1
NH = H
for h in range(H):
    s = np.array([1 if s == '#' else 0 for s in input().strip()])
    if np.all(s == pre):
        NH -= 1
    else:
        G[h, :] = s
        pre = s

H = NH
C = [[float('inf') for _ in range(W)] for _ in range(H)]
pre = ''
def dfs(r, c, cost):
    # print(' -- dfs --')
    C[r][c] = cost
    ret = deque([])
    q = deque([(r, c)])
    while q:
        cr, cc = q.popleft()
        # print('    cr = {}, cc = {}'.format(cr, cc))
        for i, j in [(0, 1), (1, 0)]:
            if not(0 <= cr + i < H and 0 <= cc + j < W):
                continue
            if C[cr+i][cc+j] != float('inf'):
                continue
            if G[r][c] == G[cr+i][cc+j]:
                C[cr+i][cc+j] = cost
                q.append((cr+i, cc+j))
                if cr+i == H-1 and cc+j == W-1:
                    return ret
            else:
                ret.append((cr+i, cc+j))
    # print(ret)
    return ret
q = deque([(0, 0)])
cost = 0
while True:
    # print('q = {}'.format(q))
    nq = deque([])
    if G[q[0][0]][q[0][1]] == 1:
        cost += 1
    for r, c in q:
        # print('  r = {}, c = {}'.format(r, c))
        if C[r][c] != float('inf'):
            continue
        nq.extend(dfs(r, c, cost))
    q = nq
    # print(C)
    # print('nq = {}'.format(nq))
    if C[H-1][W-1] != float('inf'):
        print(C[H-1][W-1])
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
H, W = [int(x) for x in input().strip().split()]
G = [0 for _ in range(H)]
C = [[float('inf') for _ in range(W)] for _ in range(H)]

for h in range(H):
    G[h] = list(input().strip())

def dfs(r, c, cost):
    # print(' -- dfs --')
    C[r][c] = cost
    ret = deque([])
    q = deque([(r, c)])
    while q:
        cr, cc = q.popleft()
        # print('    cr = {}, cc = {}'.format(cr, cc))
        for i, j in [(0, 1), (1, 0)]:
            if not(0 <= cr + i < H and 0 <= cc + j < W):
                continue
            if C[cr+i][cc+j] != float('inf'):
                continue
            if G[r][c] == G[cr+i][cc+j]:
                C[cr+i][cc+j] = cost
                q.append((cr+i, cc+j))
            else:
                ret.append((cr+i, cc+j))
    # print(ret)
    return ret

q = deque([(0, 0)])
cost = 0
while True:
    # print('q = {}'.format(q))
    nq = deque([])
    if G[q[0][0]][q[0][1]] == '#':
        cost += 1
    for r, c in q:
        # print('  r = {}, c = {}'.format(r, c))
        nq.extend(dfs(r, c, cost))
    q = nq
    # print(C)
    # print('nq = {}'.format(nq))
    if C[H-1][W-1] != float('inf'):
        print(C[H-1][W-1])
        exit()

Fixed submission:
import sys
import numpy as np
from collections import deque
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
H, W = [int(x) for x in input().strip().split()]
G = np.zeros((H, W), dtype=int)
pre = np.ones(W, dtype=int) * -1
NH = H
for h in range(H):
    s = np.array([1 if s == '#' else 0 for s in input().strip()])
    if np.all(s == pre):
        NH -= 1
    else:
        G[h, :] = s
        pre = s

H = NH
C = [[float('inf') for _ in range(W)] for _ in range(H)]
pre = ''
def dfs(r, c, cost):
    # print(' -- dfs --')
    C[r][c] = cost
    ret = deque([])
    q = deque([(r, c)])
    while q:
        cr, cc = q.popleft()
        # print('    cr = {}, cc = {}'.format(cr, cc))
        for i, j in [(0, 1), (1, 0)]:
            if not(0 <= cr + i < H and 0 <= cc + j < W):
                continue
            if C[cr+i][cc+j] != float('inf'):
                continue
            if G[r][c] == G[cr+i][cc+j]:
                C[cr+i][cc+j] = cost
                q.append((cr+i, cc+j))
                if cr+i == H-1 and cc+j == W-1:
                    return ret
            else:
                ret.append((cr+i, cc+j))
    # print(ret)
    return ret
q = deque([(0, 0)])
cost = 0
while True:
    # print('q = {}'.format(q))
    nq = deque([])
    if G[q[0][0]][q[0][1]] == 1:
        cost += 1
    for r, c in q:
        # print('  r = {}, c = {}'.format(r, c))
        if C[r][c] != float('inf'):
            continue
        nq.extend(dfs(r, c, cost))
    q = nq
    # print(C)
    # print('nq = {}'.format(nq))
    if C[H-1][W-1] != float('inf'):
        print(C[H-1][W-1])
        exit()