#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 14) + 10;
int a[2][maxn];
bool b[2][maxn] = {0};
int vis[maxn];
int g[maxn];
inline int find(int p, int x) {
  return a[p][x] == x ? x : a[p][x] = find(p, a[p][x]);
}
inline void join(int p, int x, int y) {
  x = find(p, x);
  y = find(p, y);
  if (x != y) {
    a[p][x] = y;
  }
}
void init(int p) {
  for (int i = 0; i < maxn; i++) {
    a[p][i] = i;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  int u = m / 4;
  string s;
  int flag = 0;
  int p = 0;
  int ans = 0;
  init(p);
  for (int i = 0; i <= n; i++) {
    memset(b[flag], 0, sizeof(bool) * maxn);
    memset(g, -1, sizeof(g));
    memset(vis, 0, sizeof(vis));
    if (i < n) {
      cin >> s;
      for (int j = 0; j < u; j++) {
        char c = s[j];
        int num = c <= '9' ? c - '0' : c - 'A' + 10;
        for (int k = 3; k >= 0; k--) {
          b[flag][j * 4 + k] = num & 1;
          num >>= 1;
        }
      }
    }
    int f = flag ^ 1;
    for (int j = 0; j < m; j++) {
      if (b[f][j] && b[flag][j]) vis[find(p, j)] = 1;
    }
    for (int j = 0; j < m; j++) {
      if (b[f][j] && !vis[find(p, j)]) {
        ans++;
        vis[find(p, j)] = 1;
      }
    }
    init(p ^ 1);
    for (int j = 0; j < m; j++) {
      if (b[flag][j] && b[f][j]) {
        int x = find(p, j);
        if (g[x] == -1)
          g[x] = j;
        else {
          join(p ^ 1, g[x], j);
          g[x] = j;
        }
      }
    }
    for (int j = 0; j < m - 1; j++) {
      if (b[flag][j] && b[flag][j + 1]) join(p ^ 1, j, j + 1);
    }
    flag ^= 1;
    p ^= 1;
  }
  cout << ans << endl;
  return 0;
}
