Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin python3
# -*- coding: utf-8 -*-

H, W = map(int,input().split())
sth, stw = 0, 0
glh, glw = H-1, W-1

INF = 10000
Gmap = [list(input()) for _ in range(H)]
Dist = [[INF]*W for _ in range(H)]
direc = {(1,0), (0,1)}

from collections import deque

def bfs(init):

    next_q = deque([])
    if Gmap[init[0]][init[1]]=="#":
        Dist[init[0]][init[1]] = 1
        next_q.append([0,0,"#"])
    else:
        Dist[init[0]][init[1]] = 0
        next_q.append([0,0,"."])

    while len(next_q)!=0:
        #キュー取り出し(先頭)
        h, w, fg = next_q.popleft()
        for d in direc:
            hs, ws = h + d[0], w + d[1]
            if not (0<=hs<H and 0<=ws<W):
                continue
            fgs = Gmap[hs][ws]
            if fgs=='#' and fg == '.':
                Dist[hs][ws] = min(Dist[h][w]+1, Dist[hs][ws])
            else:
                Dist[hs][ws] = min(Dist[h][w], Dist[hs][ws])
            next_q.append([hs,ws,fgs])
    return Dist

def main():
    ret = bfs([sth,stw])
    print(ret[glh][glw])

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin python3
# -*- coding: utf-8 -*-

def main():
    H, W = map(int,input().split())
    sth, stw = 0, 0
    glh, glw = H-1, W-1

    INF = 10000
    Gmap = [list(input()) for _ in range(H)]
    Dist = [[INF]*W for _ in range(H)]
    direc = {(1,0), (0,1)}

    if Gmap[0][0]=='#':
        Dist[0][0]=1
    else:
        Dist[0][0]=0

    for h in range(H):
        for w in range(W):
            nw = Gmap[h][w]
            for d in direc:
                hs, ws = h + d[0], w + d[1]
                if 0<=hs<H and 0<=ws<W:
                    cr = Gmap[hs][ws]
                    Dist[hs][ws] = min(Dist[hs][ws], Dist[h][w] + (cr=='#' and nw=='.'))
    print(Dist[glh][glw])

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin python3
# -*- coding: utf-8 -*-

H, W = map(int,input().split())
sth, stw = 0, 0
glh, glw = H-1, W-1

INF = 10000
Gmap = [list(input()) for _ in range(H)]
Dist = [[INF]*W for _ in range(H)]
direc = {(1,0), (0,1)}

from collections import deque

def bfs(init):

    next_q = deque([])
    if Gmap[init[0]][init[1]]=="#":
        Dist[init[0]][init[1]] = 1
        next_q.append([0,0,"#"])
    else:
        Dist[init[0]][init[1]] = 0
        next_q.append([0,0,"."])

    while len(next_q)!=0:
        #キュー取り出し(先頭)
        h, w, fg = next_q.popleft()
        for d in direc:
            hs, ws = h + d[0], w + d[1]
            if not (0<=hs<H and 0<=ws<W):
                continue
            fgs = Gmap[hs][ws]
            if fgs=='#' and fg == '.':
                Dist[hs][ws] = min(Dist[h][w]+1, Dist[hs][ws])
            else:
                Dist[hs][ws] = min(Dist[h][w], Dist[hs][ws])
            next_q.append([hs,ws,fgs])
    return Dist

def main():
    ret = bfs([sth,stw])
    print(ret[glh][glw])

if __name__ == '__main__':
    main()

Fixed submission:
#!/usr/bin python3
# -*- coding: utf-8 -*-

def main():
    H, W = map(int,input().split())
    sth, stw = 0, 0
    glh, glw = H-1, W-1

    INF = 10000
    Gmap = [list(input()) for _ in range(H)]
    Dist = [[INF]*W for _ in range(H)]
    direc = {(1,0), (0,1)}

    if Gmap[0][0]=='#':
        Dist[0][0]=1
    else:
        Dist[0][0]=0

    for h in range(H):
        for w in range(W):
            nw = Gmap[h][w]
            for d in direc:
                hs, ws = h + d[0], w + d[1]
                if 0<=hs<H and 0<=ws<W:
                    cr = Gmap[hs][ws]
                    Dist[hs][ws] = min(Dist[hs][ws], Dist[h][w] + (cr=='#' and nw=='.'))
    print(Dist[glh][glw])

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import sys
from collections import deque

def main():
    input = sys.stdin.readline
    H,W = map(int,input().split())
    Gd = [list(input()) for _ in range(H)]
    INF = H*W
    direc = [[1,0],[0,1]]
    def dfs(start):
        Dis = [[INF]*W for _ in range(H)]
        Dis[start[0]][start[1]]= (Gd[start[0]][start[1]] == '#')
        q = deque([])   # スタック
        q.append(start)
        while len(q)!=0:
            h,w = q.popleft()   # スタック取りだし（先頭）
            for d in direc:
                hs, ws = h + d[0], w + d[1]
                if not (0<=hs<H and 0<=ws<W):
                    continue
                else:
                    q.append([hs,ws])
                    Dis[hs][ws] = min(Dis[hs][ws], Dis[h][w] + (Gd[hs][ws]=='#'))
        print(Dis[H-1][W-1])
    dfs([0, 0])

if __name__=='__main__':
    main()


Fixed submission:
#!/usr/bin python3
# -*- coding: utf-8 -*-

def main():
    H, W = map(int,input().split())
    sth, stw = 0, 0
    glh, glw = H-1, W-1

    INF = 10000
    Gmap = [list(input()) for _ in range(H)]
    Dist = [[INF]*W for _ in range(H)]
    direc = {(1,0), (0,1)}

    if Gmap[0][0]=='#':
        Dist[0][0]=1
    else:
        Dist[0][0]=0

    for h in range(H):
        for w in range(W):
            nw = Gmap[h][w]
            for d in direc:
                hs, ws = h + d[0], w + d[1]
                if 0<=hs<H and 0<=ws<W:
                    cr = Gmap[hs][ws]
                    Dist[hs][ws] = min(Dist[hs][ws], Dist[h][w] + (cr=='#' and nw=='.'))
    print(Dist[glh][glw])

if __name__ == '__main__':
    main()