Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import math
import sys

h,w = map(int,input().split())
s = [0] * h
for i in range(h):
     s[i] = [i for i in str(input())]

box=[deque([]) for _ in range(w+h-1)]
box[0].append([0,0])
ans = 0
for i in range(w+h-1):
     while box[i]:
          y,x = box[i].popleft()
          if y == h-1 and x == w-1:
               if s[y][x] == '.' and s[0][0] == '.':
                    print(ans//2)
               elif s[y][x] == '.' and s[0][0] == '#' :
                    print((ans+1)//2)
               elif s[y][x] == '#' and s[0][0] == '.':
                    print((ans + 1) // 2)
               else:
                    print(ans//2 + 1)
               sys.exit()
          for k, j in [(1, 0), (0, 1)]:

               nexty, nextx = y + k, x + j
               if nexty < h and nextx < w:
                    if s[nexty][nextx] == s[y][x]:
                         box[i].append([nexty, nextx])

                    elif s[nexty][nextx] != s[y][x]:
                         box[i+1].append([nexty, nextx])


     ans += 1






Fixed submission:
from collections import deque
import math
import sys

h,w = map(int,input().split())
s = [0] * h
for i in range(h):
     s[i] = [i for i in str(input())]
memo = [[300] * w for _ in range(h)]
memo[0][0] = 0
box=[deque([]) for _ in range(w+h-1)]
box[0].append([0,0])
ans = 0
for i in range(w+h-1):
     while box[i]:

          y,x = box[i].popleft()
          if y == h-1 and x == w-1:
               if s[y][x] == '.' and s[0][0] == '.':
                    print(ans//2)
               elif s[y][x] == '.' and s[0][0] == '#' :
                    print((ans+1)//2)
               elif s[y][x] == '#' and s[0][0] == '.':
                    print((ans + 1) // 2)
               else:
                    print(ans//2 + 1)
               sys.exit()
          for k, j in [(1, 0), (0, 1)]:
               nexty, nextx = y + k, x + j
               if nexty < h and nextx < w:
                    if memo[nexty][nextx] > 250:
                         memo[nexty][nextx] = ans
                         if s[nexty][nextx] == s[y][x]:
                              box[i].append([nexty, nextx])

                         elif s[nexty][nextx] != s[y][x]:
                              box[i + 1].append([nexty, nextx])


     ans += 1

Buggy python submission (verdict: Runtime Error):
from collections import deque
import math
import sys

h,w = map(int,input().split())
s = [0] * h
for i in range(h):
     s[i] = [i for i in str(input())]
memo=[[math.inf] * w for _ in range(h)]
box=[deque([]) for _ in range(w+h-1)]
box[0].append([0,0])
ans = 0
for i in range(w+h-1):
     while box[i]:
          y,x = box[i].popleft()
          if y == h-1 and x == w-1:
               if s[y][x] == '.' and s[0][0] == '.':
                    print(ans//2)
               elif s[y][x] == '.' and s[0][0] == '#' :
                    print((ans+1)//2)
               elif s[y][x] == '#' and s[0][0] == '.':
                    print((ans + 1) // 2)
               else:
                    print(ans//2 + 1)
               sys.exit()
          for k, j in [(1, 0), (0, 1)]:

               nexty, nextx = y + k, x + j
               if nexty < h and nextx < w:
                    if s[nexty][nextx] == s[y][x]:
                         box[i].append([nexty, nextx])

                    elif s[nexty][nextx] != s[y][x]:
                         box[i+1].append([nexty, nextx])


     ans += 1


Fixed submission:
from collections import deque
import math
import sys

h,w = map(int,input().split())
s = [0] * h
for i in range(h):
     s[i] = [i for i in str(input())]
memo = [[300] * w for _ in range(h)]
memo[0][0] = 0
box=[deque([]) for _ in range(w+h-1)]
box[0].append([0,0])
ans = 0
for i in range(w+h-1):
     while box[i]:

          y,x = box[i].popleft()
          if y == h-1 and x == w-1:
               if s[y][x] == '.' and s[0][0] == '.':
                    print(ans//2)
               elif s[y][x] == '.' and s[0][0] == '#' :
                    print((ans+1)//2)
               elif s[y][x] == '#' and s[0][0] == '.':
                    print((ans + 1) // 2)
               else:
                    print(ans//2 + 1)
               sys.exit()
          for k, j in [(1, 0), (0, 1)]:
               nexty, nextx = y + k, x + j
               if nexty < h and nextx < w:
                    if memo[nexty][nextx] > 250:
                         memo[nexty][nextx] = ans
                         if s[nexty][nextx] == s[y][x]:
                              box[i].append([nexty, nextx])

                         elif s[nexty][nextx] != s[y][x]:
                              box[i + 1].append([nexty, nextx])


     ans += 1