Language: C++

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

const int N = 1111111;
bool prime[N];
set<int> primes;

void hurui()
{
	memset(prime, true, sizeof(prime));
	prime[0] = prime[1] = false;
	for (int i = 2; i*i < N; i++)
	{
		for (int j = 2; i*j < N; j++)
		{
			prime[i*j] = false;
		}
	}
	REP(i, N + 1)
	{
		if (prime[i]) primes.insert(i);
	}
}

const int dx[] = { -1, 0, 0, 1 }, dy[] = { 0, -1, 1, 0 };
//[const int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy[] = { 0, -1, 1, -1, 1, 0, -1, 1 };
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
int place(int x, int y, int w) { return w*x + y; }


bool fld[1200][1200] = {};
int index[1200][1200];
pii hs[1111111];

void init()
{
	REP(i, 1200)REP(j, 1200) index[i][j] = INF;
	hurui();
	int p = 1, x = 600, y = 600;
	FOR(i, 1, 11111111)
	{
		if (i % 2)
		{
			REP(j, i)
			{
				index[x][y] = p;
				hs[p] = pii(x, y);
				if (primes.count(p)) fld[x][y] = true;
				p++;
				y++;
			}
			REP(j, i)
			{
				index[x][y] = p;
				hs[p] = pii(x, y);
				if (primes.count(p)) fld[x][y] = true;
				p++;
				x--;
			}
		}
		else
		{
			REP(j, i)
			{
				index[x][y] = p;
				hs[p] = pii(x, y);
				if (primes.count(p)) fld[x][y] = true;
				p++;
				y--;
			}
			REP(j, i)
			{
				index[x][y] = p;
				hs[p] = pii(x, y);
				if (primes.count(p)) fld[x][y] = true;
				p++;
				x++;
			}
		}
		if (p > 1000000) break;
	}
}

pii dp[1200][1200];
int n, m;

pii solve(int x, int y)
{
	if (dp[x][y] !=pii(-1,-1)) return dp[x][y];
	pii res(0, -1);
	FOR(i, -1, 2)
	{
		int nx = x + 1, ny = y + i;
		if (index[nx][ny] > m) continue;
		pii next = solve(nx, ny);
		if (fld[nx][ny])
		{
			if (next.first == 0) chmax(res, pii(next.first + 1, index[nx][ny]));
			else chmax(res, pii(next.first + 1, next.second));
		}
		else chmax(res, next);
	}
	return dp[x][y] = res;
}

int main()
{
	init();
	while (cin >> m >> n, n)
	{
		REP(i, 1200)REP(j, 1200) dp[i][j] = pii(-1, -1);
		pii ans = solve(hs[n].first, hs[n].second);
		if (primes.count(n))
		{
			ans.first++;
			chmax(ans.second, n);
		}
		if (ans.first == 0) cout << "0 0" << endl;
		else cout << ans.first << " " << ans.second << endl;
	}
	return 0;
}

Compiler output:
a.cc:49:21: error: 'int index [1200][1200]' redeclared as different kind of entity
   49 | int index[1200][1200];
      |                     ^
In file included from /usr/include/string.h:462,
                 from /usr/include/c++/14/cstring:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:121,
                 from a.cc:1:
/usr/include/strings.h:50:20: note: previous declaration 'const char* index(const char*, int)'
   50 | extern const char *index (const char *__s, int __c)
      |                    ^~~~~
a.cc: In function 'void init()':
a.cc:54:39: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   54 |         REP(i, 1200)REP(j, 1200) index[i][j] = INF;
      |                                       ^
a.cc:63:38: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   63 |                                 index[x][y] = p;
      |                                      ^
a.cc:71:38: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   71 |                                 index[x][y] = p;
      |                                      ^
a.cc:82:38: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   82 |                                 index[x][y] = p;
      |                                      ^
a.cc:90:38: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   90 |                                 index[x][y] = p;
      |                                      ^
a.cc: In function 'pii solve(int, int)':
a.cc:111:26: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  111 |                 if (index[nx][ny] > m) continue;
      |                          ^
a.cc:115:82: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  115 |                         if (next.first == 0) chmax(res, pii(next.first + 1, index[nx][ny]));
      |                                                                                  ^