Buggy python submission (verdict: Time Limit Exceeded):
import sys
#input = sys.stdin.readline

def main():
    h,w=map(int,input().split())
    s=[list(input()) for _ in range(h)]
    flg=['#','.']
    def dfs(x,y,f):
        a,b=0,0
        if x<h-1:
            if s[x+1][y]==flg[f]:
                a=dfs(x+1,y,f)
            else:
                a=dfs(x+1,y,(f+1)%2)+1
        if y<w-1:
            if s[x][y+1]==flg[f]:
                b=dfs(x,y+1,f)
            else:
                b=dfs(x,y+1,(f+1)%2)+1
        return min(a,b)

    if s[0][0]=='#':
        print(dfs(0,0,0)+1)
    else:
        print(dfs(0,0,1))
        
if __name__ == '__main__':
    main()

Fixed submission:
import sys
#input = sys.stdin.readline

def main():
    h,w=map(int,input().split())
    s=[list(input()) for _ in range(h)]
    ans=[[0]*w for _ in range(h)]
    if s[0][0]=='#':
        ans[0][0]=1
    for i in range(h):
        for j in range(w):
            if i==0:
                if j==0:
                    continue
                else:
                    if s[i][j]==s[i][j-1]:
                        ans[i][j]=ans[i][j-1]
                    else:
                        ans[i][j]=ans[i][j-1]+1
            else:
                if j==0:
                    if s[i][j]==s[i-1][j]:
                        ans[i][j]=ans[i-1][j]
                    else:
                        ans[i][j]=ans[i-1][j]+1
                else:
                    if s[i][j]==s[i][j-1]:
                        a=ans[i][j-1]
                    else:
                        a=ans[i][j-1]+1
                    if s[i][j]==s[i-1][j]:
                        b=ans[i-1][j]
                    else:
                        b=ans[i-1][j]+1
                    ans[i][j]=min(a,b)
    A=ans[h-1][w-1]
    if A%2==1:
        print(A//2+1)
    else:
        print(A//2)
        
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
#input = sys.stdin.readline

def main():
    h,w=map(int,input().split())
    s=[list(input()) for _ in range(h)]
    flg=['#','.']
    def dfs(x,y,f):
        a,b=0,0
        if x<h-1:
            if s[x+1][y]==flg[f]:
                a=dfs(x+1,y,f)
            else:
                a=dfs(x+1,y,(f+1)%2)+1
        if y<w-1:
            if s[x][y+1]==flg[f]:
                b=dfs(x,y+1,f)
            else:
                b=dfs(x,y+1,(f+1)%2)+1
        return min(a,b)

    if s[0][0]=='#':
        print(dfs(0,0,0)+1)
    else:
        print(dfs(0,0,1))
        
if __name__ == '__main__':
    main()

Fixed submission:
import sys
#input = sys.stdin.readline

def main():
    h,w=map(int,input().split())
    s=[list(input()) for _ in range(h)]
    ans=[[0]*w for _ in range(h)]
    if s[0][0]=='#':
        ans[0][0]=1
    for i in range(h):
        for j in range(w):
            if i==0:
                if j==0:
                    continue
                else:
                    if s[i][j]==s[i][j-1]:
                        ans[i][j]=ans[i][j-1]
                    else:
                        ans[i][j]=ans[i][j-1]+1
            else:
                if j==0:
                    if s[i][j]==s[i-1][j]:
                        ans[i][j]=ans[i-1][j]
                    else:
                        ans[i][j]=ans[i-1][j]+1
                else:
                    if s[i][j]==s[i][j-1]:
                        a=ans[i][j-1]
                    else:
                        a=ans[i][j-1]+1
                    if s[i][j]==s[i-1][j]:
                        b=ans[i-1][j]
                    else:
                        b=ans[i-1][j]+1
                    ans[i][j]=min(a,b)
    A=ans[h-1][w-1]
    if A%2==1:
        print(A//2+1)
    else:
        print(A//2)
        
if __name__ == '__main__':
    main()