#include <fstream>
#include <vector>

using namespace std;

int main() {
  ifstream cin("video.in");
  ofstream cout("video.out");
  int n, m;
  cin >> n >> m;
  vector<vector<int>> field(n, vector<int>(m, 0));
  char c;
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
    cin >> c;
    field[i][j] = (c == '2' ? 1 : 0);
  }
  int minimalOnRow = 1000, minimalOnColumn = 1000, summary = 0, onRow, onColumn, mOnRowId = 0, mOnColumnId = 0;
  for (int i = 0; i < n; i++) {
    onRow = 0;
    for (int j = 0; j < m; j++) {
      summary += (field[i][j] + field[(i + 1) % n][j] + field[i][(j + 1) % m] + field[(i + 1) % n][(j + 1) % m]) % 4 == 0;
      onRow += (field[i][j] + field[(i + 1) % n][j] + field[i][(j + 1) % m] + field[(i + 1) % n][(j + 1) % m]) % 4 == 0;
    }
    if (onRow < minimalOnRow) {
      minimalOnRow = onRow;
      mOnRowId = i;
    }
  }
  for (int j = 0; j < m; j++) {
    onColumn = 0;
    for (int i = 0; i < n; i++) {
      onColumn += (field[i][j] + field[(i + 1) % n][j] + field[i][(j + 1) % m] + field[(i + 1) % n][(j + 1) % m]) % 4 == 0;
    }
    if (onColumn < minimalOnColumn) {
      minimalOnColumn = onColumn;
      mOnColumnId = j;
    }
  }
  cout << summary - minimalOnRow - minimalOnColumn + ((field[mOnRowId][mOnColumnId] + field[(mOnRowId + 1) % n][mOnColumnId] + field[mOnRowId][(mOnColumnId + 1) % m] + field[(mOnRowId + 1) % n][(mOnColumnId + 1) % m]) % 4 == 0);
  return 0;
}
