local mab = math.abs
local mmi, mma = math.min, math.max
local h, w = io.read("*n", "*n", "*l")
local t = {}
for i = 1, w do
  t[i] = {}
end
for i = 1, h do
  local s = io.read()
  for j = 1, w do
    t[j][i] = s:byte(j)
  end
end

local inf = 1000000007
local function solve(a, b)
  local cost = {}
  for i = 0, 2 * h + 1 do
    cost[i + 1] = {}
    for j = 0, h + 1 do
      cost[i + 1][j + 1] = 0
    end
  end
  for a_used = 0, h - 1 do
    for b_used = 0, h - 1 do
      if a[h - a_used] == b[h - b_used] then
        local sum_used = a_used + b_used
        cost[sum_used + 1][a_used + 1] = cost[sum_used + 1][a_used + 1] + 1
      end
    end
  end
  for sum_used = 2 * h, 2, -1 do
    for a_used = h, 1, -1 do
      local b_used = sum_used - a_used
      if 1 <= b_used and b_used <= h then
        cost[sum_used - 2 + 1][a_used - 1 + 1]
        = cost[sum_used - 2 + 1][a_used - 1 + 1] + cost[sum_used + 1][a_used + 1]
      end
    end
  end
  local dp1, dp2 = {}, {}
  for i = 1, h + 1 do
    dp1[i] = inf
  end
  dp1[1] = 0
  for i = 0, h * 2 - 1 do
    local a_used_min = mma(0, i - h)
    local a_used_max = mmi(h, i)
    local src = i % 2 == 0 and dp1 or dp2
    local dst = i % 2 == 0 and dp2 or dp1
    for j = 1, h + 1 do
      dst[j] = inf
    end
    for a_used = a_used_min, a_used_max do
      local b_used = i - a_used
      if a_used < h then
        dst[a_used + 2] = mmi(dst[a_used + 2], src[a_used + 1] + cost[i + 1][a_used + 1])
      end
      if b_used < h then
        dst[a_used + 1] = mmi(dst[a_used + 1], src[a_used + 1] + cost[i + 1][a_used + 1])
      end
    end
  end
  return dp1[h + 1]
end

local ret = 0
for i = 1, w - 1 do
  ret = ret + solve(t[i], t[i + 1])
end
print(ret)
