rt.
code
#include <bits/stdc++.h>
#define N 1005
using namespace std;
int n, mp[N][N], vis[N][N], ans_R = 0, ans_V = 0;
int d[8][2] = {{-1, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, -1}, {-1, 1}, {1, 1}, {1, -1}};
void bfs(int x, int y)
{
int cnt1 = 0, cnt2 = 0; vis[x][y] = 1;
queue<pair<int, int> > q; q.push(make_pair(x, y));
while (q.size())
{
int x = q.front().first, y = q.front().second; q.pop();
for (int i = 0, xx, yy; i < 8; i++)
{
xx = x + d[i][0], yy = y + d[i][1];
if (xx < 1 || xx > n || yy < 1 || yy > n) continue;
if (mp[x][y] == mp[xx][yy] && !vis[xx][yy]) vis[xx][yy] = 1, q.push(make_pair(xx, yy));
else cnt1 += mp[xx][yy] > mp[x][y], cnt2 += mp[xx][yy] < mp[x][y];
}
}
ans_R += !cnt1 && cnt2, ans_V += !cnt2 && cnt1;
return;
}
int main()
{
cin >> n;
bool flag = false;
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { cin >> mp[i][j]; if (mp[i][j] == mp[1][1]) flag = true; else flag = false; }
if (flag) { cout << 1 << ' ' << 1; return 0; }
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (!vis[i][j]) bfs(i, j);
cout << ans_R << ' ' << ans_V;
return 0;
}