program f90_add_data
    implicit none
    integer::ih,jw,nt
    integer,allocatable::data1(:),data2(:)
    integer::i,j,k,ii,jj
    integer::col,colmax
    character(len=50)::dummy
    character(len=20)::fin_1,fin_2,fout

    call getarg(1,dummy) ; read(dummy,*) jw
    call getarg(2,dummy) ; read(dummy,*) ih
    call getarg(3,fin_1)
    call getarg(4,fin_2)
    call getarg(5,fout)

    write(6,'(a)') '* Job was started.'

    nt=(jw+1)*(ih+1)
    allocate(data1(1:nt),data2(1:nt))
    do k=1,nt
        data1(k)=0
        data2(k)=0
    end do

    open(11,file=fin_1,status='old')
    k=0
    do i=1,ih+1
        read(11,'()')
        do j=1,jw+1
            k=k+1
            read(11,*) ii,jj,data1(k)
        end do
    end do
    close(11)    

    open(11,file=fin_2,status='old')
    k=0
    do i=1,ih+1
        read(11,'()')
        do j=1,jw+1
            k=k+1
            read(11,*) ii,jj,data2(k)
        end do
    end do
    close(11)    

    k=0
    colmax=0
    open(12,file=fout,status='replace')
    do i=1,ih+1 
        write(12,'(a)') '>'
        do j=1,jw+1
            col=0
            k=k+1
            col=data1(k)+data2(k)
            write(12,'(3(i10))') i-1,j-1,col
            if(colmax<col)colmax=col
        end do
    end do
    write(12,'()')
    close(12)

    write(6,*) 'colmax=',colmax
    write(6,'(a)') '** Calculation was completed.'

stop

end program f90_add_data