Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
now='.'
ans=0
x,y=0,0
if S[x][y]=='#':
  ans+=1
  now='#'
  
def findroot(x,y):
  global nextkouho,now,S
  nextkouho.add((x,y))
  if x<H-1 and S[x+1][y]==now:
    findroot(x+1,y)
  if y<W-1 and S[x][y+1]==now:
    findroot(x,y+1)
kouho=[(0,0)]
while 1:
  nextkouho=set()
  for x,y in kouho:
    if x<H-1 and S[x+1][y]==now:
      findroot(x+1,y)
    else:
      nextkouho.add((x,y))
    if y<W-1 and S[x][y+1]==now:
      findroot(x,y+1)
    else:
      nextkouho.add((x,y))
  if (H-1,W-1) in nextkouho:
    break
  if now=='#':
    now='.'
  else:
    ans+=1
    now='#'
  kouho=nextkouho
  #print(nextkouho,kouho,now,ans)
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
dp=[0]*W
if S[0][0]=='#':
  dp[0]=1
for j in range(1,W):
  if S[0][j]!=S[0][j-1] and S[0][j-1]=='.':
    dp[j]=dp[j-1]+1
  else:
    dp[j]=dp[j-1]
for i in range(1,H):
  if S[i][0]!=S[i-1][0] and S[i-1][0]=='.':
    dp[0]=dp[0]+1
  for j in range(1,W):
    hidari=1 if S[i][j]!=S[i][j-1] and S[i][j-1]=='.' else 0
    ue=1 if S[i][j]!=S[i-1][j] and S[i-1][j]=='.' else 0
    dp[j]=min(dp[j-1]+hidari,dp[j]+ue)
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
now='.'
ans=0
x,y=0,0
if S[x][y]=='#':
  ans+=1
  now='#'
  
def findroot(x,y,S,d,now):
  global nextkouho
  nextkouho.add((x,y))
  if x<H-1 and S[x+1][y]==now:
    findroot(x+1,y,S,d+1,now)
  if y<W-1 and S[x][y+1]==now:
    findroot(x,y+1,S,d+1,now)
kouho=[(0,0)]
while 1:
  nextkouho=set()
  for x,y in kouho:
    if x<H-1 and S[x+1][y]==now:
      findroot(x+1,y,S,0,now)
    else:
      nextkouho.add((x,y))
    if y<W-1 and S[x][y+1]==now:
      findroot(x,y+1,S,0,now)
    else:
      nextkouho.add((x,y))
  if (H-1,W-1) in nextkouho:
    break
  if now=='#':
    now='.'
  else:
    ans+=1
    now='#'
  kouho=list(nextkouho)
  #print(nextkouho,kouho,now,ans)
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
dp=[0]*W
if S[0][0]=='#':
  dp[0]=1
for j in range(1,W):
  if S[0][j]!=S[0][j-1] and S[0][j-1]=='.':
    dp[j]=dp[j-1]+1
  else:
    dp[j]=dp[j-1]
for i in range(1,H):
  if S[i][0]!=S[i-1][0] and S[i-1][0]=='.':
    dp[0]=dp[0]+1
  for j in range(1,W):
    hidari=1 if S[i][j]!=S[i][j-1] and S[i][j-1]=='.' else 0
    ue=1 if S[i][j]!=S[i-1][j] and S[i-1][j]=='.' else 0
    dp[j]=min(dp[j-1]+hidari,dp[j]+ue)
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
#print(S)
now='.'
ans=0
x,y=0,0
if S[x][y]=='#':
  ans+=1
  now='#'
  
def findroot(x,y,S,d,now,nextkouho):
  nextkouho.add((x,y))
  if x<H-1 and S[x+1][y]==now:
    nextkouho=findroot(x+1,y,S,d+1,now,nextkouho)
  if y<W-1 and S[x][y+1]==now:
    nextkouho=findroot(x,y+1,S,d+1,now,nextkouho)
  return nextkouho
kouho=[(0,0)]
#print(kouho)
while 1:
  nextkouho=set()
  for x,y in kouho:
    if x<H-1 and S[x+1][y]==now:
      nextkouho=findroot(x+1,y,S,0,now,nextkouho)
    else:
      nextkouho.add((x,y))
    if y<W-1 and S[x][y+1]==now:
      nextkouho=findroot(x,y+1,S,0,now,nextkouho)
    else:
      nextkouho.add((x,y))
  if (H-1,W-1) in nextkouho:
    break
  if now=='#':
    now='.'
  else:
    ans+=1
    now='#'
  kouho=list(nextkouho)
  #print(nextkouho,kouho,now,ans)
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
dp=[0]*W
if S[0][0]=='#':
  dp[0]=1
for j in range(1,W):
  if S[0][j]!=S[0][j-1] and S[0][j-1]=='.':
    dp[j]=dp[j-1]+1
  else:
    dp[j]=dp[j-1]
for i in range(1,H):
  if S[i][0]!=S[i-1][0] and S[i-1][0]=='.':
    dp[0]=dp[0]+1
  for j in range(1,W):
    hidari=1 if S[i][j]!=S[i][j-1] and S[i][j-1]=='.' else 0
    ue=1 if S[i][j]!=S[i-1][j] and S[i-1][j]=='.' else 0
    dp[j]=min(dp[j-1]+hidari,dp[j]+ue)
print(dp[-1])