Problem:
 Score : 400 points    Problem Statement Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r -th row from the top and the c -th column from the left.
Each square is painted black or white.  The grid is said to be good if and only if the following condition is satisfied:   From (1, 1) , we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square.   Note that (1, 1) and (H, W) must be white if the grid is good.  Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.   Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W) . For each pair r, c ( r_0 \leq r \leq r_1, c_0 \leq c \leq c_1 ), invert the color of (r, c) - that is, from white to black and vice versa.       Constraints  2 \leq H, W \leq 100         Input Input is given from Standard Input in the following format:  H  W  s_{11} s_{12} \cdots s_{1W}  s_{21} s_{22} \cdots s_{2W}  \vdots  s_{H1} s_{H2} \cdots s_{HW}   Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white.      Output Print the minimum number of operations needed.        Sample Input 1 3 3
.##
.#.
##.      Sample Output 1 1  Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2) , and we are done.       Sample Input 2 2 2
#.
.#      Sample Output 2 2       Sample Input 3 4 4
..##
#...
###.
###.      Sample Output 3 0  No operation may be needed.       Sample Input 4 5 5
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.      Sample Output 4 4

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    H,W=MI()
    grid = [ST() for _ in range(H)]
    N = H*W
    edge = [[] for _ in range(N)]
    def enc(h,w):
        return h*W+w

    
    
    DP = [[inf]*W for _ in range(H)]
    DP[0][0] = int(grid[0][0] == "
    for h in range(H):
        for w in range(W):
            if(h+1<H):
                if(grid[h][w] == grid[h+1][w] or grid[h+1][w] == "."):
                    DP[h+1][w] = min(DP[h+1][w], DP[h][w])
                else:
                    DP[h+1][w] = min(DP[h+1][w], DP[h][w] + 1) 
            if(w+1<W):
                if(grid[h][w] == grid[h][w+1] or grid[h][w+1] == "."):
                    DP[h][w+1] = min(DP[h][w+1], DP[h][w])
                else:
                    DP[h][w+1] = min(DP[h][w+1], DP[h][w] + 1) 
    print(DP[-1][-1])
if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque


H, W = map(int, input().split())
grid = [input() for _ in range(H)]

INF = 1000
path = [[INF] * W for _ in range(H)]

q = deque()
q.append((0, 0, 0))
path[0][0] = 0
while q:
    s, i, j = q.popleft()
    c = grid[i][j]
    for di, dj in [(1, 0), (0, 1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W:
            nc = grid[ni][nj]
            ns = s + (c != nc)
            if path[ni][nj] > ns:
                path[ni][nj] = ns
                if c == nc:
                    q.appendleft((ns, ni, nj))
                else:
                    q.append((ns, ni, nj))

ans = path[-1][-1]
ans += (grid[0][0] == '
print(ans // 2)

Solution (Python):
from collections import deque
h, w = map(int, input().split())
s_list = []
for _ in range(h):
    s = input()
    s_list.append(s)
d = deque()
count = 0
if s_list[0][0] == '
    count += 1
d.append([0, 0, count])

vis = [[101 for i in range(w)] for j in range(h)]
if s_list[0][0] == '.':
    vis[0][0] = 0
else:
    vis[0][0] = 1
for x in range(h):
    for y in range(w):
        for xx, yy in [[1, 0], [0, 1]]:
            if (0 <= x + xx < h) & (0 <= y + yy < w):
                if (s_list[x][y] == '.') & (s_list[x + xx][y + yy] == '
                    vis[x + xx][y + yy] = min(vis[x][y] + 1, vis[x + xx][y + yy])
                else:
                    vis[x + xx][y + yy] = min(vis[x][y], vis[x + xx][y + yy])          
print(vis[-1][-1])

Solution (Python):
H,W = map(int,input().split())
s = [input() for _ in range(H)]

DP = [[1000]+[0]*W for _ in range(H+1)]
re = [[0]*(W+1) for _ in range(H+1)]
DP[0] = [1000]*(W+1)
DP[0][1] = DP[1][0] = 0

for i in range(1,H+1):
    for j in range(1,W+1):
        if s[i-1][j-1] == ".":
            DP[i][j] = min(DP[i-1][j],DP[i][j-1])
        else:
            u = DP[i-1][j]+1-re[i-1][j]
            l = DP[i][j-1]+1-re[i][j-1]
            DP[i][j] = min(u, l)
            re[i][j] = 1

print(DP[H][W])

Solution (Python):
H,W=map(int,input().split())

def g(j):
    if j==".":
        return 1 
    else:
        return 0 
    
S=[]
for j in range(H):
    S.append(list(map(g,input())))


Z=[[0]*W for i in range(H)]

for y in range(H):
    if y==0:
        for x in range(W):
            if x==0:
                Z[0][0]=1-S[0][0]
            else:
                Z[y][x]=Z[y][x-1]+(S[y][x-1]==1 and S[y][x]==0)
    else:
        for x in range(W):
            if x==0:
                Z[y][x]=Z[y-1][x]+(S[y-1][x]==1 and S[y][x]==0)
            else:
                Z[y][x]=min(Z[y][x-1]+(S[y][x-1]==1 and S[y][x]==0),Z[y-1][x]+(S[y-1][x]==1 and S[y][x]==0))

print(Z[H-1][W-1])

Solution (Python):
H, W = map(int, input().split())

S = [input() for _ in range(H)]
vis = [[-1 for i in range(W)] for j in range(H)]

for v in range(H):
  for h in range(W):
    if v==0:
      if h==0:
        if S[v][h]=="
          vis[v][h]=1
        else:
          vis[v][h]=0
      else:
        if S[v][h-1]=="." and S[v][h]=="
          vis[v][h] = vis[v][h-1] + 1
        else:
          vis[v][h] = vis[v][h-1]
    else:
      if h==0:
        if S[v-1][h]=="." and S[v][h]=="
          vis[v][h] = vis[v-1][h] + 1
        else:
          vis[v][h] = vis[v-1][h]
      else:
        if S[v-1][h]=="." and S[v][h]=="
          bufv = vis[v-1][h] + 1
        else:
          bufv = vis[v-1][h]
        if S[v][h-1]=="." and S[v][h]=="
          bufh = vis[v][h-1] + 1
        else:
          bufh = vis[v][h-1]
        vis[v][h] = min(bufv, bufh)
    
    
        
print(vis[H-1][W-1])

Solution (Python):
H, W = map(int, input().split())

S = [input() for _ in range(H)]
vis = [[-1 for i in range(W)] for j in range(H)]

que = [(0,0)]
itr = 0
color = "."
if S[0][0]=="
  itr=1
  color="
vis[0][0]=itr
while len(que)>0:
  v, h = que.pop(0)
  itr = vis[v][h]
  if itr%2==0:
    color="."
  else:
    color="
  que2 = []
  if v+1 < H:
    if S[v+1][h]==color and vis[v+1][h]==-1:
      vis[v+1][h] = itr
      que2.append((v+1,h))
    elif vis[v+1][h]==-1:
      vis[v+1][h] = itr +1
      que.append((v+1,h))
  if h+1 < W:
    if S[v][h+1]==color and vis[v][h+1]==-1:
      vis[v][h+1] = itr
      que2.append((v,h+1))
    elif vis[v][h+1]==-1:
      vis[v][h+1] = itr+1
      que.append((v, h+1))
  while len(que2)>0:
    v, h = que2.pop(0)
    if v+1 < H:
      if S[v+1][h]==color and vis[v+1][h]==-1:
        vis[v+1][h] = itr
        que2.append((v+1,h))
      elif vis[v+1][h]==-1:
        vis[v+1][h] = itr +1
        que.append((v+1,h))
    if h+1 < W:
      if S[v][h+1]==color and vis[v][h+1]==-1:
        vis[v][h+1] = itr
        que2.append((v,h+1))
      elif vis[v][h+1]==-1:
        vis[v][h+1] = itr+1
        que.append((v, h+1))
    
if vis[H-1][W-1]%2 == 0:
  print(vis[H-1][W-1]//2)
else:
  print((vis[H-1][W-1]+1)//2)

Solution (Python):
def agc043_a():
    ''' 解説放送見てやってみた '''
    H, W = map(int, input().split())
    grid = []
    grid.append(['
    for _ in range(H):
        grid.append(['
    grid.append(['
    grid[1][0] = grid[H][W+1] = '.'  

    
    cost = [[10**6] * (W+2) for _ in range(H+2)]
    cost[1][0] = 0

    for i in range(1, H+1):
        for j in range(1, W+2):
            if i != H and j == W+1: continue  
            rt = cost[i][j-1]
            if grid[i][j-1] != grid[i][j]: rt += 1
            dw = cost[i-1][j]
            if grid[i-1][j] != grid[i][j]: dw += 1
            cost[i][j] = min(rt, dw)

    ans = cost[H][W+1] // 2  
    print(ans)

agc043_a()

Solution (Python):
H, W = map(int, input().split())
dp = [[H*W for __ in range(W)] for _ in range(H)]
dh = [1, 0]
dw = [0, 1]
S = []
for i in range(H):
    s = input()
    S.append(s)

if (S[0][0] == '
    dp[0][0] = 1
else:
    dp[0][0] = 0


for i in range(H):
    for j in range(W):
        for k in range(2):
            nh = i + dh[k]
            nw = j + dw[k]
            if nh >= H or nw >= W:
                continue
            add = 0
            if (S[nh][nw] == "
                add = 1
            dp[nh][nw] = min(dp[nh][nw], dp[i][j] + add)


print(dp[H-1][W-1])

Solution (Python):
h, w = map(int, input().split())
maze = [list(input()) for i in range(h)]

dp = [[0] * w for i in range(h)]
    
for i in range(h):
    for j in range(w):
        if i == j == 0:
            if maze[i][j] == "
                dp[i][j] += 1
                
        if i == 0 and j != 0:
            if maze[0][j] != maze[0][j - 1]:
                dp[0][j] = dp[0][j - 1] + 1
            else:
                dp[0][j] = dp[0][j - 1]
        
        if j == 0 and i != 0:
            if maze[i][0] != maze[i - 1][0]:
                dp[i][0] = dp[i - 1][0] + 1
            else:
                dp[i][0] = dp[i - 1][0]
        
        if i != 0 and j != 0:
            dp[i][j] = min(dp[i - 1][j] + (1 if maze[i][j] != maze[i - 1][j] else 0), dp[i][j - 1] + (1 if maze[i][j] != maze[i][j - 1] else 0))
        
        if i == h - 1 and j == w - 1:
            if maze[i][j] == "
                dp[i][j] += 1
                
ans = dp[h - 1][w - 1] // 2

print(ans)

Solution (Python):
h, w = map(int, input().split())
maze = [list(input()) for i in range(h)]

dp = [[0] * w for i in range(h)]

if maze[0][0] == "
    dp[0][0] = 1
    
for i in range(h):
    for j in range(w):
        if i == j == 0:
            continue
                
        if i == 0:
            if maze[0][j] != maze[0][j - 1]:
                dp[0][j] = dp[0][j - 1] + 1
            else:
                dp[0][j] = dp[0][j - 1]
        
        if j == 0:
            if maze[i][0] != maze[i - 1][0]:
                dp[i][0] = dp[i - 1][0] + 1
            else:
                dp[i][0] = dp[i - 1][0]
        
        if i != 0 and j != 0:
            tmp1 = 0
            tmp2 = 0
            if maze[i][j] != maze[i - 1][j]:
                tmp1 = 1
            if maze[i][j] != maze[i][j - 1]:
                tmp2 = 1
        
            dp[i][j] = min(dp[i - 1][j] + tmp1, dp[i][j - 1] + tmp2)

if maze[h - 1][w - 1] == "
    dp[h - 1][w - 1] += 1

ans = dp[h - 1][w - 1] // 2

print(ans)

Solution (Python):
from copy import deepcopy
from collections import deque

H, W = map(int, input().split())
S = [input() for _ in range(H)]

table = [[None] * W for _ in range(H)]
table[0][0] = (0 if S[0][0] == '.' else 1)

D = deque([(0, 0)])
new_D = deque()
while D:
    h, w = D.popleft()
    for dh, dw in ((1, 0), (0, 1)):
        if 0 <= h + dh < H and 0 <= w + dw < W and table[h + dh][w + dw] is None:
            if S[h + dh][w + dw] == S[h][w]:
                D.append((h + dh, w + dw))
                table[h + dh][w + dw] = table[h][w]
            else:
                new_D.append((h + dh, w + dw))
                table[h + dh][w + dw] = table[h][w] + 1
    if not D:
        D = deepcopy(new_D)
        new_D.clear()

print((table[H - 1][W - 1] + 1) // 2)

Solution (Python):

import sys


def input(): return sys.stdin.readline()[:-1] 



def solve():
    n, m = [int(x) for x in input().split()]
    dp = [[float('inf') for _ in range(m)] for _ in range(n)]
    s = []
    for i in range(n):
        s.append(input())
        
    for i in range(n):
        for j in range(m):
            c = int(s[i][j] != '.')
            if i == 0 and j == 0:
                dp[i][j] = c
            if i:
                dp[i][j] = min(dp[i][j], dp[i-1][j] + c * (s[i-1][j] != s[i][j]))
            if j:
                dp[i][j] = min(dp[i][j], dp[i][j-1] + c * (s[i][j-1] != s[i][j]))
    
    
    print(dp[n-1][m-1])
        




t = 1

for case in range(1,t+1):
    ans = solve()



"""
1 -> (1, 2) -> (2 ** 3, 3) -> (3, 1)

"""

Solution (Python):
H,W=map(int,input().split())
x=[[] for _ in range(H)]
xn=[[] for _ in range(H)]
for i in range(H):
  for _ in range(W):
    xn[i].append(999)

for i in range(H):
  x[i]=input()

xn[0][0]=0

for i in range(H+W-1):
  for j in range(i+1):
    if i-j>=W:
      continue
    if j>=H:
      break
    
    w=1
    h=1
    if i-j>=W-1:
      w=0
    if j>=H-1:
      h=0
    if w==1:
      if x[j][i-j]==".":
        if x[j][i-j+1]=="
          xn[j][i-j+1]=min(xn[j][i-j]+1,xn[j][i-j+1])
        else:
          xn[j][i-j+1]=min(xn[j][i-j],xn[j][i-j+1])
      else:
        xn[j][i-j+1]=min(xn[j][i-j],xn[j][i-j+1])
    if h==1:
      if x[j][i-j]==".":
        if x[j+1][i-j]=="
          xn[j+1][i-j]=min(xn[j][i-j]+1,xn[j+1][i-j])
        else:
          xn[j+1][i-j]=min(xn[j][i-j],xn[j+1][i-j])
      else:
        xn[j+1][i-j]=min(xn[j][i-j],xn[j+1][i-j])

if x[0][0]=="
  xn[H-1][W-1]+=1
print(xn[H-1][W-1])

Solution (Python):
import sys
input = sys.stdin.readline

H, W = map(int, input().split())
s = [input().rstrip() for _ in range(H)]

dp = [[1000]*W for _ in range(H)]

if s[0][0] == '
else: dp[0][0] = 0

for j in range(W):
    for i in range(H):
        if i <= H - 2:

            if s[i][j] == '.' and s[i+1][j] == '
                dp[i+1][j] = min(dp[i+1][j], dp[i][j] + 1)
            else: dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        if j <= W - 2:

            if s[i][j] == '.' and s[i][j+1] == '
                dp[i][j+1] = min(dp[i][j+1], dp[i][j] + 1)
            else: dp[i][j+1] = min(dp[i][j+1], dp[i][j])


print(dp[H-1][W-1])

Solution (Python):
H,W=map(int,input().split())
S=[input() for _ in range(H)]
ans=float("inf")

counter=[[999]*W for _ in range(H)]

def search(i,j,count):
    global ans

    if counter[i][j]!=999:
        ans=min(ans,count+counter[i][j])
        return(counter[i][j])

    dist=999

    if i==H-1 and j==W-1:
        ans=min(ans,count)
        return 0
    countj=count
    counti=count
    
    if j<W-1:
        if S[i][j]=="." and S[i][j+1]=="
            countj+=1
        dist=search(i,j+1,countj) + (S[i][j]=="." and S[i][j+1]=="
    if i<H-1:
        if S[i][j]=="." and S[i+1][j]=="
            counti+=1
        dist=min(dist,search(i+1,j,counti)+(S[i][j]=="." and S[i+1][j]=="
    
    counter[i][j]=dist
    return dist
search(0,0,(S[0][0]=="
print(ans)

Solution (Python):
import queue

H, W = map(int, input().split())

s = [list(input()) for _ in range(H)]

ini = 0
if s[0][0] == '
    ini = 1

dist = [[ini for _ in range(W)] for _ in range(H)]
start = (0,0)

qq = queue.Queue()

for i in range(H):
    for j in range(W):
        tgt = []
        if j == 0:
            pass
        else:
            if s[i][j-1] == '.' and s[i][j] == '
                tgt.append(dist[i][j-1] + 1)

            else:
                tgt.append(dist[i][j-1])

        if i == 0:
            pass
        else:
            if s[i-1][j] == '.' and s[i][j] == "
                tgt.append(dist[i-1][j] + 1)
            else:
                tgt.append(dist[i-1][j])
        if i + j == 0:
            continue
        dist[i][j] = min(tgt)

print(dist[H-1][W-1])

Solution (Python):
H, W = map(int, input().split())
S = [[i for i in input()] for j in range(H)]
dp = [[0 for i in range(W)] for j in range(H)]
if S[0][0] == "
    dp[0][0] = 1

for i in range(1, H):
    if S[i-1][0] == "." and S[i][0] == "
        dp[i][0] = dp[i-1][0] + 1
    else:
        dp[i][0] = dp[i-1][0]

for j in range(1, W):
    if S[0][j-1] == "." and S[0][j] == "
        dp[0][j] = dp[0][j-1] + 1
    else:
        dp[0][j] = dp[0][j-1]

for h in range(1, H):
    for w in range(1, W):

        if S[h][w] == "
            if S[h-1][w] == ".":
                c1 = dp[h-1][w] + 1
            else:
                c1 = dp[h-1][w]

            if S[h][w-1] == ".":
                c2 = dp[h][w-1] + 1
            else:
                c2 = dp[h][w-1]

            dp[h][w] = min(c1, c2)

        else:
            dp[h][w] = min(dp[h-1][w], dp[h][w-1])

print(dp[H-1][W-1])

Solution (Python):
H, W = map(int,input().split())
table = []
for h in range(H):
    table.append(input())

dp=[[1000 for i in range(W+1)] for j in range(H+1)]
dp[0][1] = 0
dp[1][0] = 0

for h in range(H):
    for w in range(W):
        dp[h+1][w+1] = min(
            dp[h][w+1] + (table[h][w]=='
            dp[h+1][w] + (table[h][w]=='
        )

print (dp[-1][-1])

Solution (Python):



def main() -> None:
    H, W = rmi()
    s = []
    for h in range(H):
        line = list(map(lambda c: c == '.', list(r())))
        s.append(line)
    dp = []
    for _ in range(H):
        dp.append([H + W + 1] * W)
    dp[0][0] = 0 if s[0][0] else 1
    for h in range(1, H):
        dp[h][0] = dp[h - 1][0] + int(s[h - 1][0] != s[h][0] and not s[h][0])
    for w in range(1, W):
        dp[0][w] = dp[0][w - 1] + int(s[0][w - 1] != s[0][w] and not s[0][w])
    for h in range(1, H):
        for w in range(1, W):
            dp[h][w] = min(dp[h - 1][w] + int(s[h - 1][w] != s[h][w] and not s[h][w]),
                           dp[h][w - 1] + int(s[h][w - 1] != s[h][w] and not s[h][w]))
    print(dp[H - 1][W - 1])


def r() -> str:
    return input().strip()


def ri() -> int:
    return int(r())


def rmi(delim: str = ' ') -> tuple:
    return tuple(map(int, input().split(delim)))


def w(data) -> None:
    print(data)


def wm(*data, delim: str = ' ') -> None:
    print(delim.join(map(str, data)))


if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10 ** 9)
    main()

Solution (Python):

h,w = map(int,input().split())

def isvalid(r,c):
    return 0 <= r < h and 0 <= c < w

g = [input() for _ in range(h)]
d = [1,0]
queue = [(0,0)]
dist = [[1000000]* w for _ in range(h)]
dist[0][0] = 0
if g[0][0] == '
    dist[0][0] = 1
while len(queue) > 0:
    next_queue = []
    for r,c in queue:
        for i in range(2):
            cos = dist[r][c]
            nr = r + d[i]
            nc = c + d[1^i]
            if not isvalid(nr,nc):
                continue
            if  g[r][c] != g[nr][nc]:
                cos += 1
            if cos < dist[nr][nc]:
                dist[nr][nc] = cos
                next_queue.append((nr,nc))
    queue = next_queue
print((dist[h-1][w-1] + 1)//2)

Solution (Python):
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
dp = [[0] * w for _ in range(h)]
if s[0][0] == "
    dp[0][0] = 1
for i in range(h):
    for j in range(w):
        if i == j == 0:
            continue
        elif i == 0:
            dp[i][j] = dp[i][j - 1]
            if not s[i][j] == s[i][j - 1]:
                dp[i][j] += 1
        elif j == 0:
            dp[i][j] = dp[i - 1][j]
            if not s[i][j] == s[i - 1][j]:
                dp[i][j] += 1
        else:
            a, b = dp[i][j - 1], dp[i - 1][j]
            if not s[i][j] == s[i][j - 1]:
                a += 1
            if not s[i][j] == s[i - 1][j]:
                b += 1
            dp[i][j] = min(a, b)
print((dp[h - 1][w - 1] - dp[0][0] + [s[0][0], s[h - 1][w - 1]].count("

Solution (Python):


h, w = list(map(int, input().split()))

ss = [list(str(input())) for _ in range(h)]

dp = [[100*100 for _ in range(w)] for i in range(h)]  

if ss[0][0] == "
    dp[0][0] = 1
else:
    dp[0][0] = 0


for h_tmp in range(0, h):
    for w_tmp in range(0, w):
        if w_tmp > 0:
            
            if ss[h_tmp][w_tmp-1] == "." and ss[h_tmp][w_tmp] == "
                dp[h_tmp][w_tmp] = min(dp[h_tmp][w_tmp], dp[h_tmp][w_tmp-1]+1)
            else:
                dp[h_tmp][w_tmp] = min(dp[h_tmp][w_tmp], dp[h_tmp][w_tmp-1])
        if h_tmp > 0:
            
            if ss[h_tmp-1][w_tmp] == "." and ss[h_tmp][w_tmp] == "
                dp[h_tmp][w_tmp] = min(dp[h_tmp][w_tmp], dp[h_tmp-1][w_tmp]+1)
            else:
                dp[h_tmp][w_tmp] = min(dp[h_tmp][w_tmp], dp[h_tmp-1][w_tmp])


print(dp[-1][-1])

Solution (Python):
inf = 1001001001
h, w = map(int, input().split())
s = []
for _ in range(h):
    s.append(input())
dp = [[inf for j in range(w)] for j in range(h)]
if s[0][0] == "
    dp[0][0] = 1
else:
    dp[0][0] = 0
for i in range(h):
    for j in range(w):
        if i+1 < h:
            count = dp[i][j]
            if s[i][j] == "." and s[i+1][j] == "
                count += 1
            dp[i+1][j] = min(dp[i+1][j], count)
        if j+1 < w:
            count = dp[i][j]
            if s[i][j] == "." and s[i][j+1] == "
                count += 1
            dp[i][j+1] = min(dp[i][j+1], count)
print(dp[h-1][w-1])

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
def main():
    h, w = map(int, input().split())
    s = [input() for _ in range(h)]
    dp = [[10**5] * w for _ in range(h)]
    def func(temp, now, flag):
        x, y = now
        if y > h - 1 or x > w - 1:
            return 10**6
        if s[y][x] == "
            if not flag:
                temp += 1
            flag = True
        else:
            flag = False
        if x == w-1 and y == h-1:
            return temp
        if dp[y][x] <= temp:
            return 10**6
        dp[y][x] = temp
        piyo = []
        return min(func(temp, (x + 1, y), flag), func(temp, (x, y + 1), flag))
    print(func(0, (0, 0), False))
main()

Solution (Python):
from collections import deque
h,w = map(int,input().split())
map_input = [list(input()) for i in range(h)]

score = [[10**10]*(w+1) for i in range(h+1)]
score[0][1] = 0
score[1][0] = 0

for i in range(h):
    for j in range(w):
        tmp1 = score[i][j+1] + (map_input[i][j]=='
        tmp2 = score[i+1][j] + (map_input[i][j]=='
        score[i+1][j+1]=min(tmp1,tmp2)
print(score[-1][-1])

Solution (Python):
import numpy as np
H,W=map(int,input().split(' '))
S = (np.array([list(input()) for i in range(H)])=='
flg=[[float("inf")]*W for i in range(H)]
flg[0][0]=S[0][0]

for i in range(H):
    for j in range(W):
        if i<H-1:
            if S[i][j]==0 and S[i+1][j]==1:
                flg[i+1][j] = min(flg[i][j]+1,flg[i+1][j])
            else:
                flg[i+1][j] = min(flg[i][j],flg[i+1][j])
        if j<W-1:
            if S[i][j]==0 and S[i][j+1]==1:
                flg[i][j+1] = min(flg[i][j]+1,flg[i][j+1])
            else:
                flg[i][j+1] = min(flg[i][j],flg[i][j+1])
print(flg[-1][-1])

Solution (Python):
import sys
sys.setrecursionlimit(10**6) 
import math
from copy import copy, deepcopy
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque 


from collections import Counter

from itertools import accumulate

from heapq import heapify,heappop,heappush




def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)

def dfs(H,W,L):
    d=0
    c=True
    if L[0][0]==False:
        d=1
        c=False
    q=deque([(d*10000,c)])
    dxs=((0,1),(1,0))
    visited=[False]*10000
    while len(q):
        
        
        dxy,c=q.popleft()
        d,xy=divmod(dxy,10000)
        x,y=divmod(xy,100)
        if visited[xy]: continue
        visited[xy]=True
        if x==H-1 and y==W-1:
            return d
        for dx in dxs:
            nx=x+dx[0]
            ny=y+dx[1]
            if nx>=H or ny>=W:
                continue
            if visited[nx*100+ny]:continue

            if L[nx][ny] or c==False:
                q.appendleft((d*10000+nx*100+ny,L[nx][ny]))
            else:
                q.append(((d+1)*10000+nx*100+ny,False))

                


def main():
    mod = 10**9+7
    

    
    H,W = map(int, input().split())
    
    L = list(list(input()) for i in range(H)) 
    
    
    for i in range (H):
        for j in range(W):
            if L[i][j]=='
                L[i][j]=False
            else:
                L[i][j]=True
            
    ans=dfs(H,W,L)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
h,w = map(int,input().split())
grid = []
for _ in range(h):
    grid.append(list(input()))
 
dp = [[0]*(w+1) for i in range(h+1)]

 
if grid[0][0] == '
    dp[0][0] += 1
 
for i in range(1,h):
    p = 0
    if grid[i][0] == '
        p += 1
    dp[i][0] = dp[i-1][0] + p
 
for i in range(1,w):
    p = 0
    if grid[0][i] == '
        p += 1
    dp[0][i] = dp[0][i-1] + p
 
for x in range(1,h):
    for y in range(1,w):
        a,b = 0,0
        if grid[x][y] == '
            a += 1
        if grid[x][y] == '
            b += 1
        dp[x][y] = min(dp[x-1][y] + a,dp[x][y-1] + b)
print(dp[h-1][w-1])

Solution (Python):
H,W = map(int,input().split())

s = []

for i in range(H):

    S = list(input())
    s.append(S)

dis = [[float("inf")] * W for i in range(H)]

if s[0][0] == "
    dis[0][0] = 1
else:
    dis[0][0] = 0

for i in range(H):

    for j in range(W):

        if i != H-1:

            if s[i][j] == "." and s[i+1][j] == "

                dis[i+1][j] = min(dis[i][j] + 1,dis[i+1][j])

            else:
                dis[i+1][j] = min(dis[i][j],dis[i+1][j])

        if j != W-1:
            if s[i][j] == "." and s[i][j+1] == "

                dis[i][j+1] = min(dis[i][j] + 1,dis[i][j+1])

            else:
                dis[i][j+1] = min(dis[i][j],dis[i][j+1])

print (dis[-1][-1])

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

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

d=deque([[0,0]])
itta=[[float('inf')]*w for i in range(h)]
if s[0][0]=="
    itta[0][0]=1
else:
    itta[0][0]=0


ans=0
for i in range(h+w-2):
    
    for j in range(len(d)):
        now=d.popleft()
        nh,nw=now
        cnt=itta[nh][nw]
        if nh+1<h:
            tmp=cnt
            if s[nh][nw]=="." and s[nh+1][nw]=="
                tmp+=1
            if itta[nh+1][nw]==float('inf'):
                d.append([nh+1,nw])
            itta[nh+1][nw]=min(itta[nh+1][nw],tmp)
        if nw+1<w:
            tmp=cnt
            if s[nh][nw]=="." and s[nh][nw+1]=="
                tmp+=1
            if itta[nh][nw+1]==float('inf'):
                d.append([nh,nw+1])
            itta[nh][nw+1]=min(itta[nh][nw+1],tmp)



print(itta[-1][-1])

Solution (Python):
H, W = map(int, input().split())
grid = [input() for _ in range(H)]
blacks = [[0]*W for _ in range(H)]

grid_to_num = {"

blacks[0][0] = grid_to_num[grid[0][0]]
for j in range(1, W):
    blacks[0][j] = blacks[0][j-1] + grid_to_num[grid[0][j]] * (1-grid_to_num[grid[0][j-1]])
for i in range(1, H):
    blacks[i][0] = blacks[i-1][0] + grid_to_num[grid[i][0]] * (1-grid_to_num[grid[i-1][0]])
    for j in range(1, W):
        blacks[i][j] = blacks[i][j-1] + grid_to_num[grid[i][j]] * (1-grid_to_num[grid[i][j-1]])
        blacks[i][j] = min(blacks[i-1][j] + grid_to_num[grid[i][j]] * (1-grid_to_num[grid[i-1][j]]), blacks[i][j])

print(blacks[H-1][W-1])

Solution (Python):

H,W = map(int,input().split())
G = [list(str(input())) for _ in range(H)]
inf = float("inf")

dist = [[inf for w in range(W)] for h in range(H)]

if G[0][0] == "
    dist[0][0] = 1
else:
    dist[0][0] = 0

for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            pass
        elif j == 0:
            dist[i][j] = dist[i-1][j]
            if G[i][j] == "
                dist[i][j]+=1
        elif i == 0:
            dist[i][j] = dist[i][j-1]
            if G[i][j] == "
                dist[i][j]+=1
        else:
            d1 = dist[i-1][j]
            d2 = dist[i][j-1]
            if G[i][j] == "
                d1+=1
            if G[i][j] == "
                d2+=1
            dist[i][j] = min(d1,d2)
        

print(dist[H-1][W-1])

Solution (Python):
h,w=map(int,input().split())
s=[]
for index in range(h):
    s.append(list(str(input())))











dp=[[-1]*w for index1 in range(h)]
if s[0][0]=='.':
    dp[0][0]=0
else:
    dp[0][0]=1

temp=s[0][0]
for index2 in range(1,w):
    if temp=='.' and s[0][index2]=='
        dp[0][index2]=dp[0][index2-1]+1
    else:
        dp[0][index2]=dp[0][index2-1]
    temp=s[0][index2]

temp=s[0][0]
for index3 in range(1,h):
    if temp=='.' and s[index3][0]=='
        dp[index3][0]=dp[index3-1][0]+1
    else:
        dp[index3][0]=dp[index3-1][0]
    temp=s[index3][0]


for i in range(1,h):
    for j in range(1,w):
        if s[i][j]=='
            if s[i-1][j]=='.':
                if s[i][j-1]=='.':
                    dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1)
                else:
                    dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1])
            else:
                if s[i][j-1]=='.':
                    dp[i][j]=min(dp[i-1][j],dp[i][j-1]+1)
                else:
                    dp[i][j]=min(dp[i-1][j],dp[i][j-1])
        else:
            dp[i][j]=min(dp[i-1][j],dp[i][j-1])


print(dp[h-1][w-1])

Solution (Python):
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

dp = [[float("inf")] * W for _ in range(H)]
if s[0][0] == "
    dp[0][0] = 1
else:
    dp[0][0] = 0

dxdy = [(1, 0), (0, 1)]
for y in range(H):
    for x in range(W):
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if nx >= W or ny >= H: continue
            if s[ny][nx] == '
                dp[ny][nx] = min(dp[ny][nx], dp[y][x] + 1)
            else:
                dp[ny][nx] = min(dp[ny][nx], dp[y][x])

print(dp[-1][-1])

Solution (Python):
H, W = map(int,input().split())
board = []
for h in range(H):
    board.append(input())

dp=[[1000 for i in range(W+1)] for j in range(H+1)]
dp[0][1] = 0
dp[1][0] = 0

for h in range(H):
    for w in range(W):
        dp[h+1][w+1] = min(
            dp[h][w+1] + (board[h][w]=='
            dp[h+1][w] + (board[h][w]=='
        )

print (dp[-1][-1])