#include <iostream>
int c[102][102];
bool visit[102][102];
int n, m;
void change(int x, int y) {
visit[x][y] = true;
//이웃한 곳으로 이동
if (x > 0 && c[x - 1][y] == 0 && !visit[x - 1][y])
change(x - 1, y);
if (y > 0 && c[x][y - 1] == 0 && !visit[x][y - 1])
change(x, y - 1);
if (x <= n && c[x + 1][y] == 0 && !visit[x + 1][y])
change(x + 1, y);
if (y <= m && c[x][y + 1] == 0 && !visit[x][y + 1])
change(x, y + 1);
//치즈가 녹는지 체크
if (c[x - 1][y] == 1) c[x - 1][y]++;
if (c[x][y - 1] == 1) c[x][y - 1]++;
if (c[x + 1][y] == 1) c[x + 1][y]++;
if (c[x][y + 1] == 1) c[x][y + 1]++;
}
int cheese() {
change(0, 0);
int res = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
switch (c[i][j]) {
case 1:
res++;
break;
default:
c[i][j] = 0;
break;
}
}
}
return res;
}
int main()
{
std::cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
std::cin >> c[i][j];
}
int res = 0;
for (int t = 1;; t++) {
for (int i = 0; i <= n + 1; i++) {
for (int j = 0; j <= m + 1; j++)
visit[i][j] = false;
}
int pres = cheese();
if (pres == 0) {
std::cout << t << std::endl << res;
break;
}
else res = pres;
}
return 0;
}