h, w = read_line.split.map(&.to_i) a = Array.new(h) { read_line.split.map(&.to_i) } top = Array.new(w, 0) ans = 0 (h - 1).times do |y| ps = w.times.select { |i| a[y][i] == 1 }.to_a 0.step(to: ps.size - 2, by: 2) do |i| a[y + 1][ps[i]] ^= 1 a[y + 1][ps[i + 1]] ^= 1 end if ps.size % 2 == 1 top[ps[-1]] += 1 end end free = 0 w.times do |x| free += top[x] // 2 top[x] %= 2 end bottom = w.times.select { |i| a[h - 1][i] == 1 }.to_a ans = 0 while bottom.size > 1 if top[bottom.last] > 0 top[bottom.last] -= 1 bottom.pop top[bottom.last] += 1 if top[bottom.last] == 2 free += 1 top[bottom.last] = 0 end bottom.pop elsif free > 0 free -= 1 top[bottom.last] = 1 bottom.pop top[bottom.last] += 1 if top[bottom.last] == 2 free += 1 top[bottom.last] = 0 end bottom.pop else ans += 1 bottom.pop end end puts ans + top.sum + free * 2 + bottom.size