#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<vector> using namespace std; struct node { int x,y; node(int xx,int yy){x=xx;y=yy;} }; int score[10][10]= { {6,6,6,6,6,6,6,6,6}, {6,7,7,7,7,7,7,7,6}, {6,7,8,8,8,8,8,7,6}, {6,7,8,9,9,9,8,7,6}, {6,7,8,9,10,9,8,7,6}, {6,7,8,9,9,9,8,7,6}, {6,7,8,8,8,8,8,7,6}, {6,7,7,7,7,7,7,7,6}, {6,6,6,6,6,6,6,6,6} }; vector<node> sd; int mp[10][10],ans; int inits; int a[10][10],b[10][10],c[10][10]; bool flag; bool isok(int x,int y,int k) { if(a[x/3*3+y/3][k]||b[x][k]||c[y][k]) return false; return true; } int t=0; void dfs(int now,int sum) { if(++t>=8000000) return; if(now==sd.size()) { flag=true; ans=max(ans,sum); return; } for(int i=1;i<=9;i++) { int x=sd[now].x; int y=sd[now].y; if(isok(x,y,i)) { a[x/3*3+y/3][i]=b[x][i]=c[y][i]=1; dfs(now+1,sum+i*score[x][y]); a[x/3*3+y/3][i]=b[x][i]=c[y][i]=0; } } } int main() { sd.clear(); flag=true; for(int i=0;i<9;i++) { for(int j=0;j<9;j++) { scanf("%d",&mp[i][j]); if(!mp[i][j]) continue; a[i/3*3+j/3][mp[i][j]]++; b[i][mp[i][j]]++; c[j][mp[i][j]]++; if(a[i/3*3+j/3][mp[i][j]]>=2||b[i][mp[i][j]]>=2||c[j][mp[i][j]]>=2) flag=false; } } inits=0; for(int i=8;flag&&i>=0;i--) for(int j=8;flag&&j>=0;j--) { if(mp[i][j]) { inits+=mp[i][j]*score[i][j]; } else sd.push_back(node(i,j)); } if(flag==false) {printf("-1\n");return 0;} ans=0; flag=false; dfs(0,inits); if(flag) printf("%d\n",ans); else printf("-1\n"); return 0; }