Language: C++

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;

vector<int>prime;
void Prime(){
    int notPrime[1000002];
    memset(notPrime,0,sizeof(notPrime));
    for(int i=2;i<2000;i++)
        for(int j=i;j*i<1000001;j++)
            notPrime[j*i]=1;
    for(int i=2;i<1000002;i++)
        if(notPrime[i]==0)
            prime.push_back(i);
}
int cav[1010][1010];
void caves(){
    memset(cav,0,sizeof(cav));
    int dir=0,x=502,y=502;
    cav[x][y]=1;
    for(int i=2;i<1000002;i++){
        if(dir==0){
            x++;
            while(cav[y-1][x]!=0){
                cav[y][x]=i;
                x++;i++;
            }
            cav[y][x]=i;
        }
        else if(dir==1){y--;
            while(cav[y][x-1]!=0){
                cav[y][x]=i;
                y--;i++;
            }
            cav[y][x]=i;
        }
        else if(dir==2){x--;
            while(cav[y+1][x]!=0){
                cav[y][x]=i;
                x--;i++;
            }
            cav[y][x]=i;
        }
        else if(dir==3){y++;
            while(cav[y][x+1]!=0){
                cav[y][x]=i;
                y++;i++;
            }
            cav[y][x]=i;
        }
        dir=(dir+1)%4;
    }
}

int dp[1010][1010];
int visited[1010][1010];
int mv[3][2]={{-1,1},{0,1},{1,1}};
int bk[3][2]={{-1,-1},{0,-1},{1,-1}};
int m,n;

bool check(int ch){
    if(ch>=0&&ch<1010)
        return true;
    return false;
}
void DP(int xx,int yy){
    queue<pair<int,int> >que;
    que.push(make_pair(xx, yy));
    while(!que.empty()){
        pair<int,int>pai=que.front();
        que.pop();
        int x=pai.first,y=pai.second;
        if(visited[y][x])
            continue;
        for(int b=0;b<3;b++){
            int mx=x+bk[b][0],my=y+bk[b][1];
            if(!(check(my)&&check(mx)))
                continue;
            if(cav[my][mx]>m){
                continue;
            }
            dp[y][x]=max(dp[y][x],dp[my][mx]);
        }
        if(binary_search(prime.begin(),prime.end(),cav[y][x])){
            dp[y][x]++;
        }
      //  cout<<"dp["<<y-yy<<"]["<<x-xx<<"] : "<<dp[y][x]<<endl;
        for(int i=0;i<3;i++){
            int nx=x+mv[i][0];
            int ny=y+mv[i][1];
            if(!(check(ny)&&check(nx)))
                continue;
            if(cav[ny][nx]>m)
                continue;
            if(visited[ny][nx])
                continue;
            if(cav[ny][nx]<=m){
                que.push(make_pair(nx, ny));
                
            }
        }
        visited[y][x]=true;
    }
}
int main(){
    Prime();
    caves();
    int x=0,y=0;
    while(cin>>m>>n,m){
        memset(dp,0,sizeof(dp));
        memset(visited,0,sizeof(visited));
        for(int i=0;i<1010;i++){
            for(int j=0;j<1010;j++){
                if(cav[i][j]==n){
                    y=i;x=j;
                    break;
                }
            }
        }
        DP(x,y);
        int ans1=0,ans2=0;
        for(int i=0;i<1010;i++){
            for(int j=0;j<1010;j++){
                if(cav[i][j]>m)continue;
                if(dp[i][j]>ans1){
                    ans1=dp[i][j];
                    ans2=cav[i][j];
                }
                if(dp[i][j]==ans1&&cav[i][j]>ans2){
                    if(!binary_search(prime.begin(),prime.end(),cav[i][j]))
                        continue;
                    ans1=dp[i][j];
                    ans2=cav[i][j];
                }
            }
        }
        if(ans1==0)ans2=0;
        cout<<ans1<<" "<<ans2<<endl;
    }
}

Compiler output:
a.cc: In function 'void Prime()':
a.cc:11:5: error: 'memset' was not declared in this scope
   11 |     memset(notPrime,0,sizeof(notPrime));
      |     ^~~~~~
a.cc:6:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    5 | #include<queue>
  +++ |+#include <cstring>
    6 | using namespace std;
a.cc: In function 'void caves()':
a.cc:21:5: error: 'memset' was not declared in this scope
   21 |     memset(cav,0,sizeof(cav));
      |     ^~~~~~
a.cc:21:5: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
a.cc: In function 'int main()':
a.cc:113:9: error: 'memset' was not declared in this scope
  113 |         memset(dp,0,sizeof(dp));
      |         ^~~~~~
a.cc:113:9: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'