program friction
  implicit none
  integer :: h, w, c(300,300) = 0, i, j, ans = 0
  character(300) :: s
  read(*,*) h, w
  do i = 1, h
    read(*,*) s
    do j = 1, w
      c(i,j) = ichar(s(j:j))-96
    end do
  end do
  do j = 1, w-1
    ans = ans+calc(h,c(1:h,j:j+1))
  end do
  write(*,'(i0)') ans
contains
  integer function calc(h,c)
    integer, intent(in) :: h, c(h,2)
    integer :: dp(0:h,0:h), cost(0:h,0:h), x, y
    dp = 0
    cost = 0
    do x = -h, h
      do y = max(1,1-x), min(h,h-x)
        cost(y,y+x) = cost(y-1,y+x-1)
        if (c(y,1) == c(y+x,2)) cost(y,y+x) = cost(y,y+x)+1
      end do
    end do
    do x = 1, h
      do y = 1, h
        dp(x,y) = min(dp(x-1,y),dp(x,y-1))+cost(x,y)
      end do
    end do
    calc = dp(h,h)
  end
end program friction