#include <bits/stdc++.h>
using namespace std;
int cur, answer;
int fa[1 << 15 + 1], temp[1 << 15 + 1];
int mat[2][1 << 14 + 1];
char row[1 << 14 + 1];
int getfa(int x) { return (x == fa[x]) ? x : fa[x] = getfa(fa[x]); }
int toNum(char ch) {
  if (isdigit(ch))
    return ch - '0';
  else
    return ch - 'A' + 10;
}
void merge(int x, int y) {
  int fx = getfa(x), fy = getfa(y);
  if (fx != fy) {
    answer--;
    fa[fy] = fx;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) fa[i] = i;
  answer = cur = 0;
  memset(mat, 0, sizeof(mat));
  for (int i = 0; i < n; i++) {
    scanf("%s", row);
    for (int j = 0; j < m / 4; j++) {
      int num = toNum(row[j]);
      for (int k = 3; k >= 0; k--)
        mat[cur][j * 4 + 3 - k] = (num & (1 << k)) ? 1 : 0;
    }
    for (int j = 0; j < m; j++) {
      if (mat[cur][j]) answer++;
      fa[j + m] = j + m;
      if (j > 0 && mat[cur][j] && mat[cur][j - 1]) merge(j + m, j + m - 1);
      if (mat[cur][j] && mat[1 - cur][j]) merge(j + m, j);
    }
    for (int j = 0; j < 2 * m; j++) temp[j] = getfa(j);
    for (int j = 0; j < m; j++)
      fa[j] = (temp[j + m] >= m) ? temp[j + m] - m : temp[j + m];
    cur = 1 - cur;
  }
  cout << answer << endl;
  return 0;
}
