require'prime' m,n,*c=$<.read.split.map &:to_i (d=[*-m..r=k=0]).map{c.map{|j|d[m]<0||j>m||d[m-j]=[d[m-j],d[m]+1].max};m-=1} p d.each{|e|k.prime?&&e>0&&r+=e;k+=1}.max+r