#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 9;
const int INF = 1e9 + 7;
const int mlog = 21;
const int SQ = 400;
int par[1 << 15];
bool a[1 << 14];
int ans, n, m;
int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }
void merge(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  --ans;
  par[x] = y;
}
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(), cout.tie();
  ;
  string s;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    for (int i = 0; i < m / 4; ++i) {
      int x = (s[i] <= '9' ? s[i] - '0' : s[i] - 'A' + 10);
      a[i * 4 + 1] = !!(x & 8);
      a[i * 4 + 2] = !!(x & 4);
      a[i * 4 + 3] = !!(x & 2);
      a[i * 4 + 4] = !!(x & 1);
    }
    for (int i = 1; i <= m; ++i) {
      if (a[i])
        par[i + m] = i + m, ++ans;
      else
        par[i + m] = 0;
    }
    for (int i = 1; i <= m; ++i) {
      if (a[i]) {
        if (par[i]) merge(i, i + m);
        if (i && a[i - 1]) merge(i + m - 1, i + m);
      }
    }
    for (int i = 1; i <= m; ++i) {
      if (a[i])
        par[i] = find(i + m) - m;
      else
        par[i] = 0;
    }
  }
  cout << ans << "\n";
}
