Why am i getting wrong answer/(Memory Limit Exceed lately)? here is my source
program timus_1029;
const
  maxk=1000000000;
  maxn=500;
  maxm=100;
type
  tcoord=record line,column:word; end;
  tart=record
             cost:longint;
             exists:boolean;
             father:tcoord;
             fee:longint;
       end;
  tm=array[1..maxm,1..maxn]of tart;
var
  a:tm;
  m,n:word;
  minlastcolumn:word;
 
procedure init_dat1;
begin
  fillchar(a,sizeof(a),0);
end;
 
procedure read_data;
var
  i,j:word;
begin
  readln(m,n);
  for i:=1 to m do
    for j:=1 to n do
      read(a[i,j].fee);
end;
 
procedure init_dat2;
var
  i:word;
begin
  for i:=1 to n do
    a[1,i].exists:=true;
end;
 
procedure art_solve;
var
  line:word;
  column:word;
begin
  for line:=2 to m do
  begin
    for column:=1 to n do
      a[line,column].cost:=maxk+1;
    for column:=1 to n do
      if a[line-1,column].exists then
        begin
          a[line,column].cost:=a[line-1,column].fee+a[line-1,column].cost;
          a[line,column].father.line:=line-1;
          a[line,column].father.column:=column;
          if (a[line,column].cost+a[line,column].fee)<=maxk then
            a[line,column].exists:=true;
        end;
    for column:=2 to n do
      if a[line,column-1].exists then
      if a[line,column].cost>(a[line,column-1].cost+a[line,column-1].fee)then
      begin
        a[line,column].cost:=a[line,column-1].cost+a[line,column-1].fee;
        a[line,column].father.line:=line;
        a[line,column].father.column:=column-1;
        if (a[line,column].cost+a[line,column].fee)<=maxk then
          a[line,column].exists:=true;
      end;
    for column:=n-1 downto 1 do
      if a[line,column+1].exists then
      if a[line,column].cost>(a[line,column+1].cost+a[line,column+1].fee)then
      begin
        a[line,column].cost:=a[line,column+1].cost+a[line,column+1].fee;
        a[line,column].father.line:=line;
        a[line,column].father.column:=column+1;
        if (a[line,column].cost+a[line,column].fee)<=maxk then
          a[line,column].exists:=true;
      end;
  end;
end;
 
procedure find_mway;
var
  i:word;
  value:longint;
begin
  value:=maxk+1;
  for i:=1 to n do
    if a[m,i].exists then
      if (a[m,i].cost+a[m,i].fee)<value then
      begin
        value:=a[m,i].cost+a[m,i].fee;
        minlastcolumn:=i;
      end;
end;
 
procedure writ_data(line,column:word);
begin
  if (line=0)or(column=0)then
    exit;
  writ_data(a[line,column].father.line,a[line,column].father.column);
  writeln(column);
end;
 
procedure write_sol;
begin
  writ_data(m,minlastcolumn);
end;
 
begin
  init_dat1;
  read_data;
  init_dat2;
  art_solve;
  find_mway;
  write_sol;
end.