#include <stdio.h>
#include <algorithm>
using namespace std;
int N, M;
char map[20][21];
int house[398][2], space[399][2];
int housen, spacen;
int ans = 400;
int main() {
scanf("%d%d", &N, &M);
for (int r = 0; r < N; ++r) {
scanf("%s", map[r]);
for (int c = 0; c < M; ++c) {
if (map[r][c] == '1') {
house[housen][0] = r;
house[housen][1] = c;
++housen;
}
else {
space[spacen][0] = r;
space[spacen][1] = c;
++spacen;
}
}
}
for (int s1 = 0; s1 < spacen; ++s1) {
for (int s2 = s1 + 1; s2 < spacen; ++s2) {
int max_cst = 0;
for (int h = 0; h < housen; ++h) {
max_cst = max(max_cst, min(abs(space[s1][0] - house[h][0]) + abs(space[s1][1] - house[h][1]), abs(space[s2][0] - house[h][0]) + abs(space[s2][1] - house[h][1])));
if (max_cst > ans) {
goto out;
}
}
ans = min(ans, max_cst);
out:
continue;
}
}
printf("%d", ans);
return 0;
}