#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define siz(x) (int)(x.size())
#define all(x) x.begin(), x.end()
#define debug_arr(x,len) for(int _=1; _<=len; _++) cout<<x[_]<<" "; cout<<'\n';
#define debug(x) cout<<'\n'<<#x<<": "<<x<<'\n';
const int maxN=1e6+5, mod=1e9+7;
int n, m, good[maxN];
vector<vector<int>>a, pref;
vector<int>snt;
void sieve()
{
for(int i=2; i<=1e6; i+=1) good[i]=1;
for(int i=2; i<=1e6; i+=1)
{
if(good[i])
{
for(int j=i*2; j<=1e6; j+=i) good[j]=0;
}
}
for(int i=2; i<=1e6; i+=1)
{
if(good[i]) snt.push_back(i);
}
}
void solve1()
{
for(int i=1; i<=n; i+=1)
{
for(int j=1; j<=m; j+=1) a[i][j]=good[a[i][j]];
}
for(int i=1; i<=n; i+=1)
{
for(int j=1; j<=m; j+=1)
{
pref[i][j]=pref[i-1][j]+pref[i][j-1]-pref[i-1][j-1]+a[i][j];
}
}
if(n>m) swap(n,m);
int ans=0;
for(int mid=0; mid<=n; mid+=1)
{
bool check=0;
for(int i=1; i<=n; i+=1)
{
for(int j=1; j<=m; j+=1)
{
if(i+mid-1>n || j+mid-1>m) continue;
int xet=pref[i+mid-1][j+mid-1]+pref[i-1][j-1]-pref[i+mid-1][j-1]-pref[i-1][j+mid-1];
if(xet>=mid*mid-1) ans=max(ans, mid*mid);
}
}
}
cout<<ans<<'\n';
}
void solve()
{
for(int i=1; i<=n; i+=1)
{
for(int j=1; j<=m; j+=1) a[i][j]=good[a[i][j]];
}
for(int i=1; i<=n; i+=1)
{
for(int j=1; j<=m; j+=1)
{
pref[i][j]=pref[i-1][j]+pref[i][j-1]-pref[i-1][j-1]+a[i][j];
}
}
// for(int i=1; i<=n; i+=1)
// {
// for(int j=1; j<=m; j+=1) cout<<a[i][j]<<" "; cout<<'\n';
// }
if(n>m) swap(n,m);
int l=-1, r=n, ans=0;
while(r-l>1)
{
int mid=(l+r)>>1;
bool check=0;
for(int i=1; i<=n; i+=1)
{
for(int j=1; j<=m; j+=1)
{
if(i+mid-1>n || j+mid-1>m) continue;
int xet=pref[i+mid-1][j+mid-1]+pref[i-1][j-1]-pref[i+mid-1][j-1]-pref[i-1][j+mid-1];
if(xet>=mid*mid-1) check=1;
}
}
// cout<<mid<<" "<<check<<'\n';
if(check) l=mid;
else r=mid;
}
bool check=0;
for(int i=1; i<=n; i+=1)
{
for(int j=1; j<=m; j+=1)
{
if(i+r-1>n || j+r-1>m) continue;
int xet=pref[i+r-1][j+r-1]+pref[i-1][j-1]-pref[i+r-1][j-1]-pref[i-1][j+r-1];
if(xet>=r*r-1) check=1;
}
}
if(check) cout<<r*r<<'\n';
else cout<<l*l<<'\n';
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
sieve();
cin>>n>>m;
a.assign(n+1, vector<int>());
pref.assign(n+1, vector<int>());
for(int i=1; i<=n; i+=1)
{
a[i].push_back(0);
for(int j=1; j<=m; j+=1)
{
int x; cin>>x;
a[i].push_back(x);
}
}
for(int i=0; i<=n; i+=1) pref[i].assign(m+1, 0);
if(n<=300 && m<=300) solve1();
else solve();
}