#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int n,m,a[22],dp[1<<20],val[1<<20];
char s[22];
int cal(int x) //将二进制字符串压缩成十进制数
{
int res = 0,b = 1;
for(int i = 0;i < m; ++i,b <<= 1) res += (s[i]=='1' ? b : 0);
return res;
}
int cal2(int a,int b) //计算a->b要花费的代价
{
int res = 0;
for(int i = 0;i < 22; ++i){
if((a & (1<<i)) == 0 && (b & (1<<i))) res++;
}
return res * res;
}
int main()
{
cin >> n >> m;
for(int i = 0;i < n; ++i) cin >>s,a[i] = cal(i);
memset(dp,inf,sizeof(dp));
dp[0] = 0;
for(int j = 0; j < (1<<n); ++j){
for(int k = 0;k < n; ++k){
if(!(j&(1<<k))){
int tep = dp[j] + cal2(val[j],a[k]);
if(dp[j|(1<<k)] > tep){
dp[j|(1<<k)] = tep;
val[j|(1<<k)] = val[j] | a[k];
}
}
}
}
cout << dp[(1<<n) - 1] ;
return 0;
}