{$IfDef Amiga}
{$F-,I+,R+,S+,V+,M 100,1,10,15}
{$EndIf}
{$IfDef WINDOWS}
Uses
	WinCrt;
{$EndIf}

Const
	MAX_DIM = 100;

	POS_TOP      = 1;
	POS_TOPRIGHT = 2;
	POS_RIGHT    = 3;
	POS_BOTRIGHT = 4;
	POS_BOT      = 5;
	POS_BOTLEFT  = 6;
	POS_LEFT     = 7;
	POS_TOPLEFT  = 8;

Type
	tMatrix = Array[1..MAX_DIM,1..MAX_DIM] of Char;
	tRowCol = Record
		r, c : Integer;
	End;

Const
	PosA : Array[POS_TOP..POS_TOPLEFT] of trowcol =
		((r : -1; c :  0),
		 (r : -1; c :  1),
		 (r :  0; c :  1),
		 (r :  1; c :  1),
		 (r :  1; c :  0),
		 (r :  1; c : -1),
		 (r :  0; c : -1),
		 (r : -1; c : -1));



Function LoCase(ch : char) : Char;

Begin
	If ch in ['A'..'Z'] then
		LoCase := Chr(ord(ch) +32)
	else
		LoCase := ch;
End;

Function LoStr(s : String) : String;

Var
	n : Integer;

Begin
	For n := 1 to Length(s) do
		s[n] := LoCase(s[n]);
	LoStr := s;
End;


Procedure GetMatrix(VAR m : tMatrix; VAR rows, cols : Integer; VAR key : String);

Var
	n, y : Integer;
	ts : String;

Begin
	Write('Enter number of rows    : ');
	Readln(rows);
	Write('Enter number of columns : ');
	Readln(cols);

	For n := 1 to rows do begin
		ts := '';
		Write('Row ',n:2,' : ');
		Readln(ts);
		If Length(ts) < cols then begin
			For y := 1 to cols-length(ts) do
				ts := ts + '-';
		End;
		For y := 1 to cols do begin
			m[n,y] := LoCase(ts[y]);
		End;
	End;
	Writeln;
	Write('Enter word to search for : ');
	Readln(key);
	key := LoStr(key);
End;

Procedure DisplayMatrix(m : tMatrix;
												rows, cols,
												srow, scol,
												erow, ecol : Integer;
												key : String);

Var
	n, y : Integer;

Const
	num : Integer = 0;

Begin
	If srow = 0 then
		Writeln('Entered Matrix:')
	else begin
		inc(num);
		Writeln('Found "',key,'" (',num,'): (Starts ',srow,',',scol,' Ends ',erow,',',ecol,')');
	End;
	{$IfDef NOTQUICK}
	For n := 1 to rows do begin
		Write(' ');
		For y := 1 to cols do begin
			If (((n=srow) and (y = scol))) or (((n=erow) and (y=ecol))) then
				Write(UpCase(m[n,y]),' ')
			else
				Write(m[n,y],' ');
		End;
	Writeln;
	End;
	{$EndIf}
End;


Procedure FindMatch(m : tMatrix;
										key : String;
										row, col : Integer;
										VAR frow, fcol : Integer);

Var
	n, y, j, w, crow, ccol, len : Integer;
	ok : Boolean;

Begin
	len := length(key);
	frow := -1;
	fcol := -1;
	If Len > 0 then begin
		For n := 1 to row do begin
			For y := 1 to col do begin
				If m[n,y] = key[1] then begin
					If len > 1 then begin
						{ found first character, find second }
						For j := POS_TOP To POS_TOPLEFT do begin
							crow := posa[j].r + n;
							ccol := posa[j].c + y;
							if ((crow <= row) and (crow > 0)) and ((ccol <= col) and(ccol > 0)) then begin
								if m[crow, ccol] = key[2] then begin
									{ found second character }
									Ok := True;
									For w := 3 to len do begin
										crow := crow + posa[j].r;
										ccol := ccol + posa[j].c;
										If ((crow > 0) and (crow <= row)) and ((ccol > 0) and (ccol <= col)) then begin
											If NOT((m[crow, ccol] = key[w]) and (OK)) then begin
												Ok := False;
											End;
										End else begin
											Ok := False;
										End;
									End;
									If Ok then
										DisplayMatrix(m, row, col, n, y, crow, ccol, key);
								End;
							End;
						End;
					End else
						DisplayMatrix(m, row, col, n, y, n, y, key)
				End;
			End;
		End;
	End;
End;




Procedure MAin;

Var
	m          : tMatrix;
	rows, cols,
	frow, fcol : Integer;
	key        : String;

Begin
	GetMatrix(m, rows, cols, key);
	DisplayMatrix(m, rows, cols, 0,0,0,0,'');
	FindMatch(m, key, rows, cols, frow, fcol);
	{$IfDef DOS}
	Readln;
	{$EndIf}
End;


Begin main End.