Buggy python submission (verdict: Time Limit Exceeded):
import sys
#input = sys.stdin.buffer.readline
 
def main():
    H,W = map(int,input().split())
    s = [list(input()) for _ in range(H)]

    def ch(x,y):
        if x == H-1 and y == W-1:
            return 0
        else:
            if s[x][y] == ".":
                if y == W-1:
                    if s[x+1][y] == ".":
                        return ch(x+1,y)
                    else:
                        return ch(x+1,y)+1
                elif x == H-1:
                    if s[x][y+1] == ".":
                        return ch(x,y+1)
                    else:
                        return ch(x,y+1)+1
                else:
                    if s[x][y] == s[x+1][y]:
                        a = ch(x+1,y)
                    else:
                        a = ch(x+1,y)+1
                    if s[x][y] == s[x][y+1]:
                        b = ch(x,y+1)
                    else:
                        b = ch(x,y+1)+1
                    return min(a,b)
            else:
                if y == W-1:
                    return ch(x+1,y)
                elif x == H-1:
                    return ch(x,y+1)
                else:
                    return min(ch(x+1,y),ch(x,y+1))

    if s[0][0] == "#":
        a = 1
    else:
        a = 0
    print(a+ch(0,0))
    
if __name__ == "__main__":
    main()


Fixed submission:
import sys
#input = sys.stdin.buffer.readline
 
def main():
    H,W = map(int,input().split())
    s = [list(input()) for _ in range(H)]
    
    memo = [[-1 for _ in range(W)] for _ in range(H)]
    def ch(x,y):
        if x == H-1 and y == W-1:
            memo[x][y] = 0
        else:
            if s[x][y] == ".":
                if y == W-1:
                    if s[x+1][y] == ".":
                        memo[x][y] = memo[x+1][y] if memo[x+1][y] != -1 else ch(x+1,y)
                    else:
                        memo[x][y] = memo[x+1][y]+1 if memo[x+1][y] != -1 else ch(x+1,y)+1
                elif x == H-1:
                    if s[x][y+1] == ".":
                        memo[x][y] = memo[x][y+1] if memo[x][y+1] != -1 else ch(x,y+1)
                    else:
                        memo[x][y] = memo[x][y+1]+1 if memo[x][y+1] != -1 else ch(x,y+1)+1
                else:
                    if s[x][y] == s[x+1][y]:
                        a = memo[x+1][y] if memo[x+1][y] != -1 else ch(x+1,y)
                    else:
                        a = memo[x+1][y]+1 if memo[x+1][y] != -1 else ch(x+1,y)+1
                    if s[x][y] == s[x][y+1]:
                        b = memo[x][y+1] if memo[x][y+1] != -1 else ch(x,y+1)
                    else:
                        b = memo[x][y+1]+1 if memo[x][y+1] != -1 else ch(x,y+1)+1
                    memo[x][y] = min(a,b)
            else:
                if y == W-1:
                    memo[x][y] = memo[x+1][y] if memo[x+1][y] != -1 else ch(x+1,y)
                elif x == H-1:
                    memo[x][y] = memo[x][y+1] if memo[x][y+1] != -1 else ch(x,y+1)
                else:
                    memo[x][y] = min(memo[x][y+1],memo[x+1][y]) if (memo[x][y+1] != -1 and memo[x+1][y] != -1) else min(ch(x+1,y),ch(x,y+1))
        return memo[x][y]

    if s[0][0] == "#":
        a = 1
    else:
        a = 0
    print(a+ch(0,0))
    
if __name__ == "__main__":
    main()