	INTEGER W(25,53),V(25,53),H,B,S,R,X,Z,Q,H1,C
	REAL LINE(26)
	DATA EDD, EBB, BBI, BBB /'!--', '!  ', '  I', '   ' /
	WRITE(1,99)
99	FORMAT(///,' RANDOM NUMBER (5 DIGITS) ')
	READ(1,98) IX
98	FORMAT(I5)
120	WRITE(1,100)
100	FORMAT(' SIZE ')
	READ(1,101) H
101	FORMAT(I2)
	B = H
	IF (H .NE. 1) GO TO 150
	IF( B .NE. 1 ) GO TO 150
	WRITE(1,102)
102	FORMAT('GIVE ME SOMETHING I CAN WORK WITH!!')
	GO TO 120
150	WRITE(1,103)
103	FORMAT(//)
	DO 10 I=1, 25
	    DO 10 J=1, 53
	 	W(I,J)=0
		V(I,J)=0
10	CONTINUE
	Q = 0
	Z = 0
	H1 = H + 1
	X = IFIX (RND(IX) * H + 1)
	DO 180 I=1, H
		IF (I .EQ. X) GO TO 173
		LINE(I) = EDD
		GO TO 180
173		LINE(I) = EBB
180	CONTINUE
	LINE(H1) = EBB
	WRITE (1,104) (LINE(I), I = 1, H1)
104	FORMAT('1',2X,26A3)
	C = 1
	W(X, 1) = C
	C = C + 1
	R = X
	S = 1
	GO TO 260
210	IF (R .NE. H) GO TO 240
	IF (S .NE. B) GO TO  230
	R=1
	S=1
	GO TO 250
230	R=1
	S=S+1
	GOTO 250
240	R=R+1
250	IF (W(R,S) .EQ. 0 ) GO TO 210
260	IF (R-1 .EQ. 0 ) GO TO  530
	IF (W(R-1,S) .NE. 0) GO TO 530
	IF (S-1 .EQ. 0) GO TO 390
	IF (W(R,S-1) .NE. 0) GO TO 390
	IF (R .EQ. H) GO TO 330
	IF (W(R+1,S) .NE. 0) GO TO 330
	X=IFIX(RND(IX)*3+1)
	GO TO (790, 820, 860), X
330	IF (S .NE. B) GO TO 340
	IF (Z .EQ. 1) GO TO 370
	Q=1
	GOTO 350
340	IF (W(R,S+1) .NE. 0) GO TO 370
350	X=IFIX(RND(IX)*3+1)
	IF (X .EQ. 1) GO TO 790
	IF (X .EQ. 2) GO TO 820
	IF (X .EQ. 3) GO TO 910
370	X=IFIX(RND(IX)*2+1)
	IF (X .EQ. 1) GO TO 790
	IF (X .EQ. 2) GO TO 820
390	IF (R .EQ. H) GO TO 470
	IF (W(R+1,S) .NE. 0) GO TO 470
	IF (S .NE. B) GO TO 420
	IF (Z .EQ. 1) GO TO 450
	Q = 1
	GOTO 430
420	IF (W(R,S+1) .NE. 0) GO TO 450
430	X = IFIX(RND(IX)*3+1)
	IF (X .EQ. 1) GO TO 790
	IF (X .EQ. 2) GO TO 860
	IF (X .EQ. 3) GO TO 910
450	X=IFIX(RND(IX)*2+1)
	IF (X .EQ. 1) GO TO 790
	IF (X .EQ. 2) GO TO 860
470	IF (S .NE. B) GO TO 490
	IF (Z .EQ. 1) GO TO 520
	Q = 1
	GOTO 500
490	IF (W(R,S+1) .NE. 0) GO TO 520
500	X=IFIX(RND(IX)*2+1)
	IF (X .EQ. 1) GO TO 790
	IF (X .EQ. 2) GO TO 910
520	GOTO 790
530	IF (S-1 .EQ. 0) GO TO 670
	IF (W(R,S-1) .NE. 0) GO TO 670
	IF (R .EQ. H) GO TO 610
	IF (W(R+1,S) .NE. 0) GO TO 610
	IF (S .NE. B) GO TO 560
	IF (Z .EQ. 1) GO TO 590
	Q=1
	GOTO 570
560	IF (W(R,S+1) .NE. 0) GO TO 590
570	X=IFIX(RND(IX)*3+1)
	IF (X .EQ. 1) GO TO 820
	IF (X .EQ. 2) GO TO 860
	IF (X .EQ. 3) GO TO 910
590	X=IFIX(RND(IX)*2+1)
	IF (X .EQ. 1) GO TO 820
	IF (X .EQ. 2) GO TO 860
610	IF (S .NE. B) GO TO 630
	IF (Z .EQ. 1) GO TO 660
	Q=1
	GOTO 640
630	IF (W(R,S+1) .NE. 0) GO TO 660
640	X=IFIX(RND(IX)*2+1)
	IF (X .EQ. 1) GO TO 820
	IF (X .EQ. 2) GO TO 910
660	GOTO 820
670	IF (R .EQ. H) GO TO 740
	IF (W(R+1,S) .NE. 0) GO TO 740
	IF (S .NE. B) GO TO 700
	IF (Z .EQ. 1) GO TO 730
	Q=1
	GOTO 830
700	IF (W(R,S+1) .NE. 0) GO TO 730
	X=IFIX(RND(IX)*2+1)
	IF (X .EQ. 1) GO TO 860
	IF (X .EQ. 2) GO TO 910
730	GOTO 860
740	IF (S .NE. B) GO TO 760
	IF (Z .EQ. 1) GO TO 780
	Q=1
	GOTO 770
760	IF (W(R,S+1) .NE. 0) GO TO 780
770	GOTO 910
780	GOTO 1000
790	W(R-1,S)=C
	C=C+1
	V(R-1,S)=2
	R=R-1
	IF (C .EQ. H*B+1) GO TO 1010
	Q=0
	GOTO 260
820	W(R,S-1)=C
830	C=C+1
	V(R,S-1)=1
	S=S-1
	IF (C .EQ. H*B+1) GO TO 1010
	Q=0
	GOTO 260
860	W(R+1,S)=C
	C=C+1
	IF (V(R,S) .EQ. 0) GO TO 880
	V(R,S)=3
	GOTO 890
880	V(R,S)=2
890	R=R+1
	IF (C .EQ. H*B+1) GO TO 1010
	GOTO 530
910	IF (Q .EQ. 1) GO TO 960
	W(R,S+1)=C
	C=C+1
	IF (V(R,S) .EQ. 0) GO TO 940
	V(R,S)=3
	GOTO 950
940	V(R,S)=1
950	S=S+1
	IF (C .EQ. H*B+1) GO TO 1010
	GOTO 260
960	Z=1
	IF (V(R,S) .EQ. 0) GO TO 980
	V(R,S)=3
	Q=0
	GOTO 1000
980	V(R,S)=1
	Q=0
	R=1
	S=1
	GOTO 250
1000	GOTO 210
1010	DO 1073 J=1, B
		LINE(1) = BBI
		DO 1040 I=1, H
			IF (V(I,J) .LT. 2) GO TO 1030
			LINE(I+1) = BBB
			GOTO 1040
1030			LINE(I+1) = BBI
1040		CONTINUE
		WRITE(1, 106) (LINE(I), I=1, H1)
106		FORMAT (1X,26A3)
		DO 1070 I=1, H
			IF (V(I,J) .EQ. 0) GO TO 1060
			IF (V(I,J) .EQ. 2) GO TO 1060
			LINE(I) = EBB
			GOTO 1070
1060			LINE(I) = EDD
1070		CONTINUE
		LINE(H1) = EBB
		WRITE (1,107) (LINE(I), I = 1, H1)
107		FORMAT (3X, 26A3)
1073	CONTINUE
	STOP
	END
