#include <iostream>
#include <cstring>
#define MAX_WIDTH 8
#define WALL 6
#define BLINDSPOT 0
using namespace std;
typedef struct CCTV {
int row;
int col;
int direction; // 1 : ๋ถ, 2 : ๋, 3 : ๋จ, 4 : ์
int num;
} CCTV;
int height, width;
int map[MAX_WIDTH][MAX_WIDTH];
int tempMap[MAX_WIDTH][MAX_WIDTH];
CCTV cctv[MAX_WIDTH];
int numOfCctv;
int minBlindSpot(CCTV c[MAX_WIDTH], int len = 0);
int calcBlindSpot(CCTV c[MAX_WIDTH]);
void up(int i, int j);
void right(int i, int j);
void down(int i, int j);
void left(int i, int j);
int main() {
cin >> height >> width;
int index = 0;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++) {
int temp;
cin >> temp;
map[i][j] = temp;
if (temp != 0 && temp != 6) {
cctv[index].row = i;
cctv[index].col = j;
cctv[index].direction = 0;
cctv[index++].num = temp;
}
}
numOfCctv = index;
cout << minBlindSpot(cctv);
}
int minBlindSpot(CCTV c[MAX_WIDTH], int len) {
if (len == numOfCctv)
return calcBlindSpot(c);
int temp[4];
int last = c[len].num != 1 ? c[len].num != 2 ? c[len].num != 3 ? c[len].num != 4 ? 1 : 4 : 4 : 2 : 4;
for (int i = 1; i <= last; i++) {
c[len].direction = i;
temp[i-1] = minBlindSpot(c, len + 1);
}
int min = temp[0];
for (int i = 1; i < last; i++)
min = temp[i] < min ? temp[i] : min;
return min;
}
int calcBlindSpot(CCTV c[MAX_WIDTH]) {
for (int i = 0; i < height; i++)
memcpy(tempMap[i], map[i], sizeof(int) * width);
for (int i = 0; i < numOfCctv; i++) {
if (c[i].num == 5) {
up(c[i].row, c[i].col);
right(c[i].row, c[i].col);
down(c[i].row, c[i].col);
left(c[i].row, c[i].col);
}
else if (c[i].num == 2) {
if (c[i].direction == 1) {
right(c[i].row, c[i].col);
left(c[i].row, c[i].col);
}
else if (c[i].direction == 2) {
up(c[i].row, c[i].col);
down(c[i].row, c[i].col);
}
}
else {
if (c[i].num == 1) {
if (c[i].direction == 1)
up(c[i].row, c[i].col);
else if (c[i].direction == 2)
right(c[i].row, c[i].col);
else if (c[i].direction == 3)
down(c[i].row, c[i].col);
else if (c[i].direction == 4)
left(c[i].row, c[i].col);
}
else if (c[i].num == 3) {
if (c[i].direction == 1) {
up(c[i].row, c[i].col);
right(c[i].row, c[i].col);
}
else if (c[i].direction == 2) {
right(c[i].row, c[i].col);
down(c[i].row, c[i].col);
}
else if (c[i].direction == 3) {
down(c[i].row, c[i].col);
left(c[i].row, c[i].col);
}
else if (c[i].direction == 4) {
left(c[i].row, c[i].col);
up(c[i].row, c[i].col);
}
}
else if (c[i].num == 4) {
if (c[i].direction == 1) {
up(c[i].row, c[i].col);
right(c[i].row, c[i].col);
left(c[i].row, c[i].col);
}
else if (c[i].direction == 2) {
up(c[i].row, c[i].col);
right(c[i].row, c[i].col);
down(c[i].row, c[i].col);
}
else if (c[i].direction == 3) {
left(c[i].row, c[i].col);
right(c[i].row, c[i].col);
down(c[i].row, c[i].col);
}
else if (c[i].direction == 4) {
left(c[i].row, c[i].col);
down(c[i].row, c[i].col);
up(c[i].row, c[i].col);
}
}
}
}
int count = 0;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
if (tempMap[i][j] == 0)
count++;
return count;
}
void up(int i, int j) {
for (int k = i - 1; k >= 0; k--) {
if (tempMap[k][j] != 6)
tempMap[k][j] = -1;
else
break;
}
}
void right(int i, int j) {
for (int k = j + 1; k < width; k++) {
if (tempMap[i][k] != 6)
tempMap[i][k] = -1;
else
break;
}
}
void down(int i, int j) {
for (int k = i+ 1; k < height; k++) {
if (tempMap[k][j] != 6)
tempMap[k][j] = -1;
else
break;
}
}
void left(int i, int j) {
for (int k = j - 1; k >= 0; k--) {
if (tempMap[i][k] != 6)
tempMap[i][k] = -1;
else
break;
}
}