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):
from itertools import product
from collections import deque

def zero_one_bfs(E, s, init=0):
    N = len(E)
    order = [-1] * N 
    dist = [-1] * N 
    dq = deque([(s, init)]) 
    num = 0 
    while dq:
        v, d = dq.popleft()
        if order[v] < 0: 
            order[v] = num; dist[v] = d
            num += 1
            for u, w in E[v]:
                if order[u] >= 0: continue
                if w == 0: dq.appendleft((u, d))
                else: dq.append((u, d+1))
    return order, dist

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

def vtx(i, j): return i*W + j
def coord(n): return divmod(n, W)

grid = [input() for _ in range(H)] 
E = [[] for _ in range(H * W)]
for i, j in product(range(H), range(W)):
    v = vtx(i, j)
    check = [vtx(i+dx, j+dy) for dx, dy in [(1, 0), (0, 1)] if i+dx <= H-1 and j+dy <= W-1]
    for u in check:
        x, y = coord(u)
        if grid[i][j] == '.' and grid[x][y] == '
            E[v].append((u, 1))
        elif grid[i][j] == '
            E[v].append((u, 0))
        else:
            E[v].append((u, 0))
_, dist = zero_one_bfs(E, 0, 0 if grid[0][0] == '.' else 1)
print(dist[vtx(H-1, W-1)])

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

dp = list(list(float('INF') for _ in range(W)) for _ in range(H))
dp[0][0] = 1 if field[0][0] == '

for x in range(W):
    for y in range(H):
        if y < H - 1:
            dp[y + 1][x] = min(dp[y + 1][x],
                               dp[y][x] + 1 if field[y + 1][x] == '
        if x < W - 1:
            dp[y][x + 1] = min(dp[y][x + 1],
                               dp[y][x] + 1 if field[y][x + 1] == '

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

Solution (Python):
def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    ans = [[float("inf")] * (W) for _ in range(H)]
    if S[0][0] == "
        ans[0][0] = 1
    else:
        ans[0][0] = 0
    for i in range(H):
        for j in range(W):
            tmp = ans[i][j]
            if 0 < i:
                tmp = min(tmp, ans[i-1][j] +
                          (1 if S[i-1][j] == "." and S[i][j] == "
            if 0 < j:
                tmp = min(tmp, ans[i][j-1] +
                          (1 if S[i][j-1] == "." and S[i][j] == "
            ans[i][j] = tmp
    print(ans[H-1][W-1])


if __name__ == "__main__":
    main()

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

dist = [[H*W for i in range(W)] for j in range(H)]

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

for h in range(H):
    for w in range(W):
        if w + 1 < W:
            if s[h][w] == "." and s[h][w+1] == "
                dist[h][w+1] = min(dist[h][w+1], dist[h][w] + 1)
            else:
                dist[h][w+1] = min(dist[h][w+1], dist[h][w])
        if h + 1 < H:
            if s[h][w] == "." and s[h+1][w] == "
                dist[h+1][w] = min(dist[h+1][w], dist[h][w] + 1)
            else:
                dist[h+1][w] = min(dist[h+1][w], dist[h][w])

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

Solution (Python):
h,w = map(int,input().split())
s = [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 == 0)&(j != 0):
            dp[i][j] = dp[i][j-1] + (s[i][j-1] != s[i][j])*1
        elif(i != 0)&(j == 0):
            dp[i][j] = dp[i-1][j] + (s[i-1][j] != s[i][j])*1
        elif(i != 0)&(j != 0):
            dp[i][j] =min(dp[i][j-1] + (s[i][j-1] != s[i][j])*1,
                            dp[i-1][j] + (s[i-1][j] != s[i][j])*1)

ans = (dp[-1][-1] + 1)//2
print(ans)

Solution (Python):
import sys

stdinr = sys.stdin.readline

ns = lambda: stdinr().rstrip()
ni = lambda: int(stdinr().rstrip())
nm = lambda: map(int, stdinr().split())
nl = lambda: list(map(int, stdinr().split()))

h,w = nm()
g = [list(map(lambda s:s=='
dp = [[h*w]*w for _ in range(h)]
dp[0][0] = g[0][0]
for i in range(h):
    for j in range(w):
        if i < h-1:
            d = dp[i][j] + (g[i][j] < g[i+1][j])
            if dp[i+1][j] > d: dp[i+1][j] = d
        if j < w-1:
            d = dp[i][j] + (g[i][j] < g[i][j+1])
            if dp[i][j+1] > d: dp[i][j+1] = d
print(dp[-1][-1])

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()

cost = [[INF] * (W + 1) for i in range(H + 1)]

field = ["

for i in range(H):
    line = input()
    field.append("

if field[1][1] == "
    cost[1][1] = 1
else:
    cost[1][1] = 0

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if i * j == 1:
            continue
        
        if field[i][j] == ".":
            cost[i][j] = min(cost[i-1][j], cost[i][j-1])
        else:
            up_cost = cost[i-1][j]
            left_cost = cost[i][j-1]
            
            if field[i-1][j] == ".":
                up_cost += 1
            if field[i][j-1] == ".":
                left_cost += 1
            
            cost[i][j] = min(up_cost, left_cost)
print(cost[H][W])

Solution (Python):
from collections import deque
H,W = map(int,input().split())
s = []
for i in range(H):
    s.append(list(input()))

que = deque()
alr = [[float('inf') for i in range(W)] for j in range(H)]
if s[0][0] == '.':
    alr[0][0] = 0
else:
    alr[0][0] = 1
que.append((0,0))
while que:
    h,w = que.popleft()
    if s[h][w] == '
        flag = True
    else:
        flag = False

    if h < H-1:
        if s[h+1][w] == '.':
            if alr[h+1][w] > alr[h][w]:
                alr[h+1][w] = alr[h][w]
                que.append((h+1,w))
        else:
            if flag:
                if alr[h+1][w] > alr[h][w]:
                    alr[h+1][w] = alr[h][w]
                    que.append((h+1,w))
            else:
                if alr[h+1][w] > alr[h][w]+1:
                    alr[h+1][w] =alr[h][w]+1
                    que.append((h+1,w))            
    if w < W-1:
        if s[h][w+1] == '.':
            if alr[h][w+1] > alr[h][w]:
                alr[h][w+1] =alr[h][w]
                que.append((h,w+1))
        else:
            if flag:
                if alr[h][w+1] > alr[h][w]:
                    alr[h][w+1] =alr[h][w]
                    que.append((h,w+1))
            else:
                if alr[h][w+1] > alr[h][w]+1:
                    que.append((h,w+1))
                    alr[h][w+1] = alr[h][w] + 1
print(alr[-1][-1])

Solution (Python):

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

def cost(h, w):
    return 1 if S[h][w] == '

inf = 100000


dp = [[inf] * W for _ in range(H)]
dp[0][0] = cost(0, 0)
for w in range(1, W):
    
    dp[0][w] = dp[0][w - 1] + int(S[0][w - 1] == '.' and S[0][w] == '

for h in range(1, H):
    
    dp[h][0] = dp[h - 1][0] + int(S[h - 1][0] == '.' and S[h][0] == '
    for w in range(1, W):
        
        if S[h][w] == '.':
            dp[h][w] = min(dp[h][w - 1], dp[h - 1][w])
        else:
            dp[h][w] = min(dp[h][w - 1] + int(S[h][w - 1] == '.'), dp[h - 1][w] + int(S[h - 1][w] == '.'))



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

Solution (Python):
def main():
  import sys
  input=sys.stdin.buffer.readline
  h,w=map(int,input().split())
  b,q=b'.'*w,range(w)
  for i in range(h):
    s=input()
    a=[]
    a_add=a.append
    for x,y,z,c in zip(b,b'.'+s,s,q):
      if x==46>z:c+=1
      if y==46>z:i+=1
      if i>c:i=c
      a_add(i)
    b,q=s,a
  print(i)
main()

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

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

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

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

Solution (Python):
def main():
  import sys
  input=sys.stdin.buffer.readline
  h,w=map(int,input().split())
  b,q=b'.'*w,range(w)
  for i in range(h):
    s=input()
    a=[]
    for x,y,z,c in zip(b,b'.'+s,s,q):
      c+=x==46>z
      i+=y==46>z
      if i>c:i=c
      a+=i,
    b,q=s,a
  print(i)
main()

Solution (Python):
import sys
import copy
import math
import fractions

import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
import numpy as np
from decimal import *

maiz = []
maxh = 0
maxw = 0


def CheckCost(h, w):
    if h + 1 < maxh:
        if costList[h + 1][w] == -1 or costList[h + 1][w] > costList[h][w] + CheckColum(h + 1, w ,maiz[h][w]):
            costList[h + 1][w] = costList[h][w] + CheckColum(h + 1, w ,maiz[h][w])
            CheckCost(h + 1, w)

    if w + 1 < maxw:
        if costList[h][w + 1] == -1 or costList[h][w + 1] > costList[h][w] + CheckColum(h, w + 1,maiz[h][w]):
            costList[h][w + 1] = costList[h][w] + CheckColum(h, w + 1,maiz[h][w])
            CheckCost(h, w + 1)


def CheckColum(h, w ,s):
    if maiz[h][w] == "
        return 1
    else:
        return 0


if __name__ == '__main__':
    h, w = map(int, input().split())
    maxh = h
    maxw = w
    costList = []
    for i in range(h):
        costList.append([-1] * w)

    for i in range(h):
        s = input()
        maiz.append(list(map(lambda x: x, s)))
    if maiz[0][0] == "
        costList[0][0] = 1
    else:
        costList[0][0] = 0

    CheckCost(0, 0)

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

Solution (Python):
def main():
  _,*s=open(0)
  b,q='.'*101,range(101)
  for i,s in enumerate(s):
    a=[]
    for x,y,z,c in zip(b,'.'+s,s,q):
      c+=x=='.'>z
      i+=y=='.'>z
      if i>c:i=c
      a+=i,
    b,q=s,a
  print(a[-2])
main()

Solution (Python):
_,*s=open(0)
b,q='.'*101,range(101)
for i,s in enumerate(s):
  a=[i]
  for x,y,z,c in zip(b,'.'+s,s,q):a+=min(c+(x=='.'>z),a[-1]+(y=='.'>z)),
  b,q=s,a[1:]
print(a[-2])

Solution (Python):
_,*s=open(0)
b,q='.'*101,range(101)
for i,s in enumerate(s):
  a=[i]
  for x,y,z,c in zip(b,'.'+s,s,q):
    a+=min(c+(x=='.'>z),a[-1]+(y=='.'>z)),
  b,q=s,a[1:]
print(a[-2])

Solution (Python):
import numpy as np

h, w = map(int, input().split())
grid = np.array([list(input()) for _ in range(h)])

dp = np.full((h+1, w+1), h+w, dtype=np.int)

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

print(dp[h, w])

Solution (Python):
H,W=map(int,input().split())
s=[list(input()) for i in range(H)]
dp=[[10**6 for j in range(W)] for i 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>0:
            if (s[i][j],s[i-1][j])==("
                dp[i][j]=min(dp[i][j],dp[i-1][j]+1)
            else:
                dp[i][j]=min(dp[i][j],dp[i-1][j])
        if j>0:
            if (s[i][j],s[i][j-1])==("
                dp[i][j]=min(dp[i][j],dp[i][j-1]+1)
            else:
                dp[i][j]=min(dp[i][j],dp[i][j-1])
print(dp[-1][-1])

Solution (Python):
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
inf = 10 ** 9
dp = [[inf] * W for _ in range(H)]
dp[0][0] = 0 if s[0][0] == '.' else 1

for y in range(H):
    for x in range(W):
        if y < H - 1:
            dp[y + 1][x] = min(dp[y + 1][x], dp[y][x] +
                               (1 if s[y][x] == '.' and s[y + 1][x] == '
        if x < W - 1:
            dp[y][x + 1] = min(dp[y][x + 1], dp[y][x] +
                               (1 if s[y][x] == '.' and s[y][x + 1] == '

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

Solution (Python):
h, w = map(int, input().split())
m = []
for i in range(h):
    inp = input()
    tmpa = inp.replace('.', '0')
    tmpb = tmpa.replace('
    m.append(tmpb)


ans = [[0] * w for i in range(h)]
for y in range(h):
    for x in range(w):
        if y == 0 and x == 0:
            if m[0][0] == '1':
                ans[0][0] += 1

        elif y == 0:
            ans[y][x] = ans[y][x - 1]
            if m[y][x - 1] == '0' and m[y][x] == '1':
                ans[y][x] += 1

        elif x == 0:
            ans[y][x] = ans[y - 1][x]
            if m[y - 1][x] == '0' and m[y][x] == '1':
                ans[y][x] += 1

        else:
            ans[y][x] = min((ans[y][x - 1] + int(bool(m[y][x - 1] == '0' and m[y][x] == '1')),
                             ans[y - 1][x] + int(bool(m[y - 1][x] == '0' and m[y][x] == '1'))))

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

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


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

dp = [[float("inf") for _ in range(W)] for _ in range(H)]

dp[0][0] = 1 if S[0][0] == "
d = [(0, 1), (1, 0)]
for i in range(H):
    for j in range(W):
        for dx, dy in d:
            ni = i + dx
            nj = j + dy
            if ni < H and nj < W:
                if S[i][j] == "
                    tmp = dp[i][j]
                else:
                    if S[ni][nj] == "
                        tmp = dp[i][j] + 1
                    else:
                        tmp = dp[i][j]
                if tmp < dp[ni][nj]:
                    dp[ni][nj] = tmp

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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    H, W = LI()
    g = []
    for _ in range(H):
        g.append(S())
    dp = [[inf for _ in range(W)] for __ in range(H)]

    if g[0][0] == '
        dp[0][0] = 1
    else:
        dp[0][0] = 0

    for r in range(H):
        for c in range(W):
            if r == 0 and c == 0:
                if g[r][c] == '
                    dp[r][c] = 1
                else:
                    dp[r][c] = 0
 
            cand1, cand2 = inf, inf
            if 0 <= r-1 < H:
                if g[r-1][c] == '.' and g[r][c] == '
                    dp[r][c] = min(dp[r][c], dp[r-1][c] + 1)
                else:
                    dp[r][c] = min(dp[r][c], dp[r-1][c])
            if 0<= c-1 < W:
                if g[r][c-1] == '.' and g[r][c] == '
                    dp[r][c] = min(dp[r][c], dp[r][c-1] + 1)
                else:
                    dp[r][c] = min(dp[r][c], dp[r][c-1])
    print(dp[-1][-1])
main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    H, W = LI()
    g = []
    for _ in range(H):
        g.append(S())
    dp = [[inf for _ in range(W)] for __ in range(H)]

    if g[0][0] == '
        dp[0][0] = 1
    else:
        dp[0][0] = 0

    for r in range(H):
        for c in range(W):
            if r == 0 and c == 0:
                if g[r][c] == '
                    dp[r][c] = 1
                else:
                    dp[r][c] = 0
 
            cand1, cand2 = inf, inf
            if r-1 >= 0:
                if g[r-1][c] == '.' and g[r][c] == '
                    dp[r][c] = min(dp[r][c], dp[r-1][c] + 1)
                else:
                    dp[r][c] = min(dp[r][c], dp[r-1][c])
            if c-1 >= 0:
                if g[r][c-1] == '.' and g[r][c] == '
                    dp[r][c] = min(dp[r][c], dp[r][c-1] + 1)
                else:
                    dp[r][c] = min(dp[r][c], dp[r][c-1])
    print(dp[-1][-1])
main()

Solution (Python):
def main():
    H, W = map(int, input().split())
    s = []
    for _ in range(H):
        S = input()
        s.append(S)
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 if s[0][0] == '
    for i in range(1, W):
        dp[0][i] = dp[0][i-1] + 1 if s[0][i] == '
    for i in range(1, H):
        dp[i][0] = dp[i-1][0] + 1 if s[i][0] == '
    for i in range(1, H):
        for j in range(1, W):
            if s[i][j] == '.':
                dp[i][j] = min(dp[i][j-1], dp[i-1][j])
            else:
                u = dp[i-1][j]
                r = dp[i][j-1]
                if s[i-1][j] == '.':
                    u += 1
                if s[i][j-1] == '.':
                    r += 1
                dp[i][j] = min(u, r)
    return dp[-1][-1]

print(main())

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    H, W = LI()
    g = []
    for _ in range(H):
        g.append(S())
    dp = [[inf for _ in range(W)] for __ in range(H)]

    if g[0][0] == '
        dp[0][0] = 1
    else:
        dp[0][0] = 0

    for r in range(H):
        for c in range(W):
            cand1, cand2 = inf, inf
            if r-1 >= 0:
                if g[r-1][c] == '.' and g[r][c] == '
                    dp[r][c] = min(dp[r][c], dp[r-1][c] + 1)
                else:
                    dp[r][c] = min(dp[r][c], dp[r-1][c])
            if c-1 >= 0:
                if g[r][c-1] == '.' and g[r][c] == '
                    dp[r][c] = min(dp[r][c], dp[r][c-1] + 1)
                else:
                    dp[r][c] = min(dp[r][c], dp[r][c-1])
    print(dp[-1][-1])
main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    H, W = LI()
    g = []
    for _ in range(H):
        g.append(S())
    dp = [[inf for _ in range(W)] for __ in range(H)]

    if g[0][0] == '
        dp[0][0] = 1
    else:
        dp[0][0] = 0

    for r in range(H):
        for c in range(W):
            if r != H-1:
                if g[r][c] == '.' and g[r+1][c] == '
                    dp[r+1][c] = min(dp[r+1][c], dp[r][c] + 1)
                else:
                    dp[r+1][c] = min(dp[r+1][c], dp[r][c])
            if c != W-1:
                if g[r][c] == '.' and g[r][c+1] == '
                    dp[r][c+1] = min(dp[r][c+1], dp[r][c] + 1)
                else:
                    dp[r][c+1] = min(dp[r][c+1], dp[r][c])
    print(dp[-1][-1])
main()

Solution (Python):
h, w = map(int, input().split())
lst = [['*' for i in range(w+2)]]
for i in range(h):
    lsti = ['*']
    lsti.extend(list(input()))
    lsti.append('*')
    lst.append(lsti)
lst.append(['*' for i in range(w+2)])

lst_cnt = [[300 for i in range(w+2)] for j in range(h+2)]
if lst[1][1] == '
else: lst_cnt[1][1] = 0

for i in range(1, h+1):
    for j in range(1, w+1):
        if i == 1 and j == 1: continue
        if lst[i][j] == '.':
            lst_cnt[i][j] = min(abs(lst_cnt[i-1][j]), abs(lst_cnt[i][j-1]))
        else:
            if lst[i-1][j] == '
                kouho1 = lst_cnt[i-1][j]
            else:
                kouho1 = -(lst_cnt[i-1][j]+1)
            if lst[i][j-1] == '
                kouho2 = lst_cnt[i][j-1]
            else:
                kouho2 = -(lst_cnt[i][j-1]+1)
            lst_cnt[i][j] = -min(abs(kouho1), abs(kouho2))

print(abs(lst_cnt[h][w]))

Solution (Python):
from collections import deque
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
dist = [[10**6 for _ in range(W)] for _ in range(H)]

dist[0][0] = 0

q = deque([(0, 0)])
while q:
	h, w = q.pop()

	if h < H-1:
		if s[h][w] == "." and s[h+1][w] == "
			if dist[h+1][w] > dist[h][w] + 1:
				dist[h+1][w] = dist[h][w] + 1
				q.appendleft((h+1, w))
		else:
			if dist[h+1][w] > dist[h][w]:
				dist[h+1][w] = min(dist[h+1][w], dist[h][w])
				q.append((h+1, w))

	if w < W-1:
		if s[h][w] == "." and s[h][w+1] == "
			if dist[h][w+1] > dist[h][w] + 1:
				dist[h][w+1] = dist[h][w] + 1
				q.appendleft((h, w+1))
		else:
			if dist[h][w+1] > dist[h][w]:
				dist[h][w+1] = dist[h][w]
				q.append((h, w+1))


print(dist[H-1][W-1] + (s[0][0] == "

Solution (Python):
inf=float("inf")
h,w=map(int,input().split())
D=[[inf]*w for _ in range(h)]
D[0][0]=0

S=[list(input()) for _ in range(h)]
for i in range(h):
    for j in range(w):
        for x,y in ((1,0),(0,1)):
            X,Y=i+x,j+y
            if 0<=X<h and 0<=Y<w:
                if S[i][j]=="." and S[X][Y]=="
                else:D[X][Y]=min(D[i][j],D[X][Y])



ans=D[h-1][w-1]
print(ans if S[0][0]!="

Solution (Python):
h,w=map(int,input().split())
d=[0]+[300]*w
for i in range(h):
  s=input()
  if i==0:
    a=s[0]
  for j in range(w):
    t=s[j]==a
    d[j]+=t^(d[j]%2==0)
    if j>0:
      d[j]=min(d[j],d[j-1]+t^(d[j-1]%2==0))
print((d[-2]+(a=="

Solution (Python):
def main():
  H,W = map(int,input().split())
  S = [input() for _ in range(H)]
  
  dp = [[float('inf') for _ in range(W)] for _ 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 j > 0:
        if S[i][j-1] == '.' and S[i][j] == '
          dp[i][j] = min(dp[i][j],dp[i][j-1]+1)
        else:
          dp[i][j] = min(dp[i][j],dp[i][j-1])
      if i > 0:
        if S[i-1][j] == '.' and S[i][j] == '
          dp[i][j] = min(dp[i][j],dp[i-1][j]+1)
        else:
          dp[i][j] = min(dp[i][j],dp[i-1][j])
  print(dp[H-1][W-1])

main()

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

dp=[[1000]*(w+1) for _ in range(h+1)]


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

for i in range(h):
    for j in range(w):
        if i-1>=0:

            dp[i][j]=dp[i-1][j]+(1 if s[i-1][j]=='.' and s[i][j]=='
        if j-1>=0:

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

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

Solution (Python):
INF = 10**10


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

def next_field (a):
    ret = []
    if a[0] < H-1 :
        ret.append((a[0] + 1, a[1]))
    
    if a[1] < W-1:
        ret.append((a[0], a[1] + 1))
    
    return ret

field = {}
dist = {}
for h in range(H):
    f = input()
    for w, c in enumerate(f):
        field[(h,w)] = c
        dist[(h,w)] = INF

s = set()

if field[(0,0)] == ".":
    dist[(0,0)] = 0
else:
    dist[(0,0)] = 1

s.add((0,0))

while len(s) > 0:
    cur = s.pop()

    for nxt in next_field(cur):

        if field[cur] == field[nxt] or field[nxt] == ".":
            dist[nxt] = min(dist[nxt], dist[cur])
        else:
            dist[nxt] = min(dist[nxt], 1 + dist[cur])

        s.add(nxt)
    
print(dist[(H-1,W-1)])

Solution (Python):
from itertools import count, repeat

h = int(input().split()[0])
a0 = repeat('.')
r0 = count()
for i in range(h):
  a = input()
  r = [i]
  for x, y, z, u in zip(a0, '.' + a, a, r0):
    r.append(min(u + (x > z), r[-1] + (y > z)))
  a0 = a
  r0 = r[1:]
print(r[-1])

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
from re import split
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy
import re

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


h, w = LI()
L = SR(h)
dp = [[INF] * w for _ in range(h)]
dp[0][0] = 1 if L[0][0] == '
for y in range(h):
    for x in range(w):
        if y:
            if L[y][x] == '.' or L[y - 1][x] == '
                dp[y][x] = dp[y - 1][x]
            else:
                dp[y][x] = dp[y - 1][x] + 1
        if x:
            if L[y][x] == '.' or L[y][x - 1] == '
                dp[y][x] = min(dp[y][x], dp[y][x - 1])
            else:
                dp[y][x] = min(dp[y][x], dp[y][x - 1] + 1)


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

Solution (Python):
from itertools import count, repeat

h = int(input().split()[0])
a0 = repeat('.')
r0 = count()
for i in range(h):
  a = input()
  r = [i]
  for x, y, z, u in zip(a0, '.' + a, a, r0):
    r.append(min(u + (x + z == '.
  a0 = a
  r0 = r[1:]
print(r[-1])

Solution (Python):
from pprint import pprint
h, w = map(int, input().split())

s = []
for i in range(h):
    s.append([1 if i == "
cost = [[0]*w for _ in range(h)]
cost[0][0] = s[0][0]

for i in range(1, h):
    if s[i-1][0] == 0 and s[i][0] == 1:
        cost[i][0] = cost[i-1][0] + 1
    else:
        cost[i][0] = cost[i-1][0]
for i in range(1, w):
    if s[0][i-1] == 0 and s[0][i] == 1:
        cost[0][i] = cost[0][i-1] + 1
    else:
        cost[0][i] = cost[0][i-1]

for i in range(1, h):
    for j in range(1, w):
        if s[i-1][j] == 0 and s[i][j] == 1:
            cost_down = cost[i-1][j] + 1
        else:
            cost_down = cost[i-1][j]
        if s[i][j-1] == 0 and s[i][j] == 1:
            cost_right = cost[i][j-1] + 1
        else:
            cost_right = cost[i][j-1]
        cost[i][j] = min(cost_down, cost_right)

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

Solution (Python):
h, w = map(int, input().split())
a0 = '.' * w
r0 = range(w)
for i in range(h):
  a = input()
  r = [i]
  for x, y, z, u in zip(a0, '.' + a, a, r0):
    r.append(min(u + (x + z == '.
  a0 = a
  r0 = r[1:]
print(r[-1])

Solution (Python):
h, w = map(int, input().split())
a0 = '.' * w
r0 = list(range(w))
for i in range(h):
  a = input()
  r = [i]
  for x, y, z, u in zip(a0, '.' + a, a, r0):
    r.append(min(u + (x + z == '.
  a0 = a
  r0 = r[1:]
print(r[-1])

Solution (Python):
h,w = map(int,input().split())
L = []
dp = []
for i in range(h):
    d = [0]*w
    dp.append(d)
M = []
for i in range(h):
    s = input()
    M.append(s)
if M[0][0] == '
    dp[0][0] = 1
for i in range(h):
    for j in range(w):
        if i == 0 and j == 0:
            None
        elif i == 0:
            if M[i][j-1] == '.' and M[i][j] == '
                dp[i][j] = dp[i][j-1]+1
            else:
                dp[i][j] = dp[i][j-1]
        elif j == 0:
            if M[i-1][j] == '.' and M[i][j] == '
                dp[i][j] = dp[i-1][j]+1
            else:
                dp[i][j] = dp[i-1][j]
        else:
            k = 0
            l = 0
            if M[i-1][j] == '.' and M[i][j] == '
                k = 1
            if M[i][j-1] == '.' and M[i][j] == '
                l = 1
            dp[i][j] = min(dp[i-1][j]+k, dp[i][j-1]+l)
print(dp[h-1][w-1])