local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[stagenum] = {}
  end
  self.stagenum = stagenum
  self.left_stage = {}
  for i = 1, n do
    local sp, sz = 1, bls(1, stagenum - 1)
    while(i - 1) % sz ~= 0 do
      sp, sz = sp + 1, brs(sz, 1)
    end
    self.left_stage[i] = sp
  end
  self.sz_stage = {}
  local tmp, sp = 1, stagenum
  for i = 1, n do
    if tmp * 2 == i then tmp, sp = tmp * 2, sp - 1 end
    self.sz_stage[i] = sp
  end
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.right_bound = function(self, val, left, right)
  local retpos = left - 1
  local l, r = left, right
  local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
  local stagenum = self.stagenum
  while true do
    local sz = bls(1, stagenum - stage)
    if self.stage[stage][mce(l / sz)] < val then
      retpos = l + sz - 1
      if retpos == right then break end
      if l + sz <= r then
        l = l + sz
        stage = mma(self.left_stage[l], self.sz_stage[r - l + 1])
      else break end
    else
      if sz ~= 1 then stage, r = stage + 1, l + sz - 2
      else break end
    end
  end
  return retpos + 1
end
SegTree.left_bound = function(self, val, left, right)
  local retpos = right + 1
  local stage, l, r = 1, left, right
  local stagenum = self.stagenum
  while true do
    local sz = bls(1, stagenum - stage)
    while r % sz ~= 0 or r + 1 - l < sz do
      stage = stage + 1
      sz = bls(1, stagenum - stage)
    end
    if self.stage[stage][mfl(r / sz)] < val then
      retpos = r - sz + 1
      if l + sz <= r then stage, l, r = 1, l, r - sz
      else break end
    else
      if sz ~= 1 then stage, l, r = stage + 1, r - sz + 2, r
      else break end
    end
  end
  return retpos - 1
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, m = io.read("*n", "*n", "*l")
local pos = {0}
do
  local s = io.read()
  local i = 1
  for d in s:gmatch("%d+") do
    pos[i + 1] = pos[i] + tonumber(d)
    i = i + 1
  end
end
local imos = {}
for i = 1, n do
  imos[i] = {}
  for j = i, n do
    imos[i][j - i + 1] = 0
  end
end
local t = {}

for i = 1, m do
  t[i] = {}
end
for i = 1, n do
  local s = io.read()
  local j = 1
  for d in s:gmatch("%d+") do
    t[j][i] = tonumber(d)
    j = j + 1
  end
end
local idx = {}
for i = 1, n do idx[i] = i end

local st = SegTree.new(n, mma, 0)
local stend = st.stage[st.stagenum]
for i = 1, m do
  table.sort(idx, function(a, b) return t[i][a] < t[i][b] end)
  for j = 1, n do
    stend[idx[j]] = j
  end
  st:updateAll()
  for j = 1, n - 1 do
    local tj = t[i][j]
    local rk = stend[j]
    local right = st:right_bound(rk + 1, j + 1, n) - 1
    local left = j == 1 and 1 or st:left_bound(rk + 1, 1, j - 1) + 1
    imos[left][j - left + 1] = imos[left][j - left + 1] + tj
    imos[j + 1][1] = imos[j + 1][1] - tj
    if right < n then
      imos[left][right - left + 2] = imos[left][right - left + 2] - tj
      imos[j + 1][right + 1 - j] = imos[j + 1][right + 1 - j] + tj
    end
  end
  do
    local tj = t[i][n]
    local rk = stend[n]
    local left = st:left_bound(rk + 1, 1, n - 1) + 1
    imos[left][n - left + 1] = imos[left][n - left + 1] + tj
  end
end
for i = 1, n do
  for j = 2, n - i + 1 do
    imos[i][j] = imos[i][j] + imos[i][j - 1]
  end
end
for i = 2, n do
  for j = 1, n - i + 1 do
    imos[i][j] = imos[i][j] + imos[i - 1][j + 1]
  end
end

local score = 0
for i = 1, n do
  local imi, posi = imos[i], pos[i]
  for j = i, n do
    score = mma(score, imi[j - i + 1] - pos[j] + posi)
  end
end
print(score)


Measured memory_bytes: 209348