Eight Directions Crossword
First you can change those non-required letters to a specific character like '.'.

Turning the 2D array into a string or array of byte. For example:

1234
5678
90ab
cdef
=&gt;
string1: 1234
string2: 5678
string3: 90ab
string4: cdef


Remove '.' in these string so that later u can check faster.

Also doing this with other rows, column, and oblique.

Scan result in -> or &lt;- direction only.

Related algorithm you may find here.

Moreover, you can omit some impossible cases such as:

input:

8 aaaa
aaaaoooo
oooooooo
oooooooo
oooooooo
oooooooo
oooooooo
oooooooo
oooooooo


Find those which never touch required characters like the last three rows in this case and  do not trag() with it.

I suggest using this algorithm to scan it:

For rows:

var 
    ignore: array[1..1000, 1..1000] of boolean;
    counter: integer;
counter:=0;
for i:=1 to n do
    while j&lt;=n do
    begin
        ignore[i, j]:=false;
        if kar[i, j] = '.' then
        begin
             count:=count+1;
             ignore[i, j]:=counter&gt; length(s);
             break;
        end
        else
            counter:=0;
    end;