#include <bits/stdc++.h>   using namespace std;   long long n, m, b[110][10010], i, j, k, minn, l, p, w, ind;   string a[10005];   int main() {     cin >> n >> m;     p = false;     w = false;     for (i = 0; i < n; i++) {       k = -1;       cin >> a[i];       for (j = 0; j < m; j++) {         if (a[i][j] ==  1 ) {           p = true;           b[i][j] = 0;           k = 0;           k++;           ind = j;         } else {           b[i][j] = k;           if (k > 0) k++;         }       }       j = 0;       while (b[i][j] != 0) {         b[i][j] = m - ind + j;         j++;       }       if (p == false) {         w = true;         break;       }       p = false;       k = -1;       for (j = m - 1; j >= 0; j--) {         if (a[i][j] ==  1 ) {           b[i][j] = 0;           k = 0;           k++;           ind = j;         } else {           if (b[i][j] < 0 || (k < b[i][j] && k >= 0)) b[i][j] = k;           if (k > 0) k++;         }       }       j = m - 1;       while (b[i][j] != 0) {         if (b[i][j] > ind + m - j) b[i][j] = ind + m - j;         j--;       }     }     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         b[i][j] = min(b[i][j], m - b[i][j]);       }     }     minn = 9999999;     for (i = 0; i < m; i++) {       l = 0;       for (j = 0; j < n; j++) l = l + b[j][i];       if (l < minn) minn = l;     }     if (w == true)       cout <<  -1 ;     else       cout << minn;   }   