#include <bits/stdc++.h>
using namespace std;
int N, M, h[502][502], qh = 0, cnt = 0, br = 1, b[502], fl[502][502];
struct xd {
int l = 501, r = 0;
} f[502];
void dfs(int px, int py, int i)
{
fl[px][py] = 1;
if (px-1 && h[px][py] > h[px-1][py]) {
if (px-1 == N) {
b[py] = 1;
if (py < f[i].l) f[i].l = py;
if (py > f[i].r) f[i].r = py;
}
if (!fl[px-1][py]) dfs(px-1, py, i);
}
if (px+1<=N && h[px][py] > h[px+1][py]) {
if (px+1 == N) {
b[py] = 1;
if (py < f[i].l) f[i].l = py;
if (py > f[i].r) f[i].r = py;
}
if (!fl[px+1][py]) dfs(px+1, py, i);
}
if (py-1 && h[px][py] > h[px][py-1]) {
if (px == N) {
b[py-1] = 1;
if (py-1 < f[i].l) f[i].l = py-1;
if (py-1 > f[i].r) f[i].r = py-1;
}
if (!fl[px][py-1]) dfs(px, py-1, i);
}
if (py+1<=M && h[px][py] > h[px][py+1]) {
if (px == N) {
b[py+1] = 1;
if (py+1 < f[i].l) f[i].l = py+1;
if (py+1 > f[i].r) f[i].r = py+1;
}
if (!fl[px][py+1]) dfs(px, py+1, i);
}
}
int main()
{
cin >> N >> M;
for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) cin >> h[i][j];
for (int i = 1; i <= M; ++i) if (h[1][i-1] <= h[1][i] && h[1][i+1] <= h[1][i]) {
dfs(1, i, i);
memset(fl, 0, sizeof(fl));
}
for (int i = 1; i <= M; ++i) br &= b[i];
if (br) {
int x = 1;
while (x <= M) {
int maxr = 0;
for (int i = 1; i <= M; ++i) if (f[i].l <= x && maxr < f[i].r) maxr = f[i].r;
x = maxr + 1;
++cnt;
}
} else {
for (int i = 1; i <= M; ++i) if (!b[i]) ++cnt;
}
printf("%d\n%d\n", br, cnt);
return 0;
}