Language: C++

#define SIZE 2000
#define X first
#define Y second
  
const int STX = 1000, STY = 1000;
  
int id[SIZE][SIZE];
bool is_prime[MAX_N+1];
vector<pair<int, int> > coord;

void Sieve() {
  fill(is_prime, is_prime+MAX_N+1, true);
  is_prime[0] = is_prime[1] = false;
  for(int i=2; i*i<=MAX_N; i++) {
    if(is_prime[i]) {
      for(int j=i*i; j<=MAX_N; j+=i) {
	is_prime[j] = false;
      }
    }
  }
}

void MakeMap() {
  int x = STX, y = STY;
  int cnt = 0;
  
  coord.PB(MP(0, 0)); // no use
  cnt ++;
  coord.PB(MP(x, y)); // cnt = 1
  
  fill(id[0], id[0]+SIZE*SIZE+1, -1);
  
  const int dx[] = {0,-1,0,1};
  const int dy[] = {-1,0,1,0};
  
  int a = 0; x = STX+1; y = STY+1;
  while(cnt <= MAX_N) {
    a += 2;
    for(int turn=0; turn<4; turn++) {
      for(int i=0; i<a; i++) {
	x += dx[turn], y += dy[turn];
	coord.PB(MP(x, y));
	id[y][x] = ++ cnt;
      }
    }
    x++, y++;
  }
}

int dp[MAX_N+1];

int main() {
  
  Sieve();
  MakeMap();
  
  int m, n;

  while(cin >> m >> n && (m|n)) {
    
    //bool ok = false;
    
    fill(dp, dp+MAX_N+1, -1);
    dp[n] = is_prime[n];
    pair<int, int> ans(dp[n], n);
    
    queue<pair<int, int> > que;
    que.push(coord[n]);
    queue<int> q_dp;
    q_dp.push(dp[n]);
    while(!que.empty()) {
      pair<int, int> now = que.front(); que.pop();
      int tmp = q_dp.front(); q_dp.pop();
      int tmpid = id[now.Y][now.X];
      if(tmp < dp[tmpid]) continue;
      
      int noid = id[now.Y][now.X];
      
      for(int i=-1; i<=1; i++) {
	pair<int, int> nex = MP(now.X+i, now.Y+1);
	if(nex.X < 0 || SIZE <= nex.X || nex.Y < 0 || SIZE <= nex.Y) continue;
	int neid = id[nex.Y][nex.X];
	if(neid <= m) {
	  if(dp[neid] < dp[noid] + is_prime[neid]) {
	    dp[neid] = dp[noid] + is_prime[neid];
	    //if(is_prime[neid]) ok = true;
	    ans = max(ans, make_pair(dp[neid], neid));
	    que.push(nex);
	    q_dp.push(dp[neid]);
	  }
	}
      }

    }
    
    /*
    int res = dp[n], lastid = -1;
    if(is_prime[n]) lastid = n;
    
    for(int i=1; i<=m; i++) {
      if(res <= dp[i] && is_prime[i]) {
	res = dp[i];
	lastid = max(lastid, i);
      }
    }
    */
    if(ans.first == 0) cout << "0 0" << endl;
    else {
      int ans2 = 2;
      cout << ans.first << " ";
      for(int i=1; i<=m; i++) {
	if(ans.first == dp[i] && is_prime[i]) {
	  ans2 = max(ans2, i);
	}
      }
      cout << ans2 << endl;
    }
  }
  
  return 0;
}

Compiler output:
a.cc:8:15: error: 'MAX_N' was not declared in this scope
    8 | bool is_prime[MAX_N+1];
      |               ^~~~~
a.cc:9:1: error: 'vector' does not name a type
    9 | vector<pair<int, int> > coord;
      | ^~~~~~
a.cc: In function 'void Sieve()':
a.cc:12:8: error: 'is_prime' was not declared in this scope
   12 |   fill(is_prime, is_prime+MAX_N+1, true);
      |        ^~~~~~~~
a.cc:12:27: error: 'MAX_N' was not declared in this scope
   12 |   fill(is_prime, is_prime+MAX_N+1, true);
      |                           ^~~~~
a.cc:12:3: error: 'fill' was not declared in this scope
   12 |   fill(is_prime, is_prime+MAX_N+1, true);
      |   ^~~~
a.cc: In function 'void MakeMap()':
a.cc:27:3: error: 'coord' was not declared in this scope
   27 |   coord.PB(MP(0, 0)); // no use
      |   ^~~~~
a.cc:27:12: error: 'MP' was not declared in this scope
   27 |   coord.PB(MP(0, 0)); // no use
      |            ^~
a.cc:31:3: error: 'fill' was not declared in this scope
   31 |   fill(id[0], id[0]+SIZE*SIZE+1, -1);
      |   ^~~~
a.cc:37:16: error: 'MAX_N' was not declared in this scope
   37 |   while(cnt <= MAX_N) {
      |                ^~~~~
a.cc: At global scope:
a.cc:50:8: error: 'MAX_N' was not declared in this scope
   50 | int dp[MAX_N+1];
      |        ^~~~~
a.cc: In function 'int main()':
a.cc:59:9: error: 'cin' was not declared in this scope
   59 |   while(cin >> m >> n && (m|n)) {
      |         ^~~
a.cc:63:10: error: 'dp' was not declared in this scope
   63 |     fill(dp, dp+MAX_N+1, -1);
      |          ^~
a.cc:63:17: error: 'MAX_N' was not declared in this scope
   63 |     fill(dp, dp+MAX_N+1, -1);
      |                 ^~~~~
a.cc:63:5: error: 'fill' was not declared in this scope
   63 |     fill(dp, dp+MAX_N+1, -1);
      |     ^~~~
a.cc:64:13: error: 'is_prime' was not declared in this scope
   64 |     dp[n] = is_prime[n];
      |             ^~~~~~~~
a.cc:65:5: error: 'pair' was not declared in this scope
   65 |     pair<int, int> ans(dp[n], n);
      |     ^~~~
a.cc:65:10: error: expected primary-expression before 'int'
   65 |     pair<int, int> ans(dp[n], n);
      |          ^~~
a.cc:67:5: error: 'queue' was not declared in this scope
   67 |     queue<pair<int, int> > que;
      |     ^~~~~
a.cc:67:16: error: expected primary-expression before 'int'
   67 |     queue<pair<int, int> > que;
      |                ^~~
a.cc:68:5: error: 'que' was not declared in this scope
   68 |     que.push(coord[n]);
      |     ^~~
a.cc:68:14: error: 'coord' was not declared in this scope
   68 |     que.push(coord[n]);
      |              ^~~~~
a.cc:69:11: error: expected primary-expression before 'int'
   69 |     queue<int> q_dp;
      |           ^~~
a.cc:70:5: error: 'q_dp' was not declared in this scope
   70 |     q_dp.push(dp[n]);
      |     ^~~~
a.cc:72:12: error: expected primary-expression before 'int'
   72 |       pair<int, int> now = que.front(); que.pop();
      |            ^~~
a.cc:74:22: error: 'now' was not declared in this scope
   74 |       int tmpid = id[now.Y][now.X];
      |                      ^~~
a.cc:80:14: error: expected primary-expression before 'int'
   80 |         pair<int, int> nex = MP(now.X+i, now.Y+1);
      |              ^~~
a.cc:81:12: error: 'nex' was not declared in this scope
   81 |         if(nex.X < 0 || SIZE <= nex.X || nex.Y < 0 || SIZE <= nex.Y) continue;
      |            ^~~
a.cc:82:23: error: 'nex' was not declared in this scope
   82 |         int neid = id[nex.Y][nex.X];
      |                       ^~~
a.cc:87:13: error: 'ans' was not declared in this scope
   87 |             ans = max(ans, make_pair(dp[neid], neid));
      |             ^~~
a.cc:87:28: error: 'make_pair' was not declared in this scope
   87 |             ans = max(ans, make_pair(dp[neid], neid));
      |                            ^~~~~~~~~
a.cc:87:19: error: 'max' was not declared in this scope
   87 |             ans = max(ans, make_pair(dp[neid], neid));
      |                   ^~~
a.cc:107:8: error: 'ans' was not declared in this scope
  107 |     if(ans.first == 0) cout << "0 0" << endl;
      |        ^~~
a.cc:107:24: error: 'cout' was not declared in this scope
  107 |     if(ans.first == 0) cout << "0 0" << endl;
      |                        ^~~~
a.cc:107:41: error: 'endl' was not declared in this scope
  107 |     if(ans.first == 0) cout << "0 0" << endl;
      |                                         ^~~~
a.cc:110:7: error: 'cout' was not declared in this scope
  110 |       cout << ans.first << " ";
      |       ^~~~
a.cc:113:18: error: 'max' was not declared in this scope
  113 |           ans2 = max(ans2, i);
      |                  ^~~
a.cc:116:23: error: 'endl' was not declared in this scope
  116 |       cout << ans2 << endl;
      |                       ^~~~