Cuộc khủng hoảng kinh tế toàn cầu nổi tiếng là nhanh chóng tiếp cận, vì vậy các bang Berman, Berance và Bertaly hình thành một liên minh và cho phép các cư dân của tất cả các quốc gia thành viên được tự do đi qua lãnh thổ của bất kỳ của họ. Ngoài ra, nó đã được quyết định rằng một con đường giữa các quốc gia cần phải được xây dựng để đảm bảo quá một mà có bất kỳ điểm nào của bất cứ nước nào có thể đạt được từ bất kỳ điểm nào của bất kỳ nước nào khác. Từ khi con đường luôn đắt tiền, các chính phủ của các tiểu bang của liên minh mới được thành lập yêu cầu bạn để giúp họ đánh giá chi phí. Để làm điều này, bạn đã được ban hành một bản đồ có thể được biểu diễn như một bảng chữ nhật gồm n hàng và cột m. Bất kỳ tế bào của bản đồ, hoặc thuộc về một trong ba trạng thái, hoặc là một khu vực mà nó được cho phép để xây dựng một con đường, hoặc là một khu vực nơi xây dựng nền đường là không được phép. Một tế bào được gọi là khá, nếu nó thuộc về một trong những tiểu bang, hoặc đường được xây dựng trong các tế bào này. Từ bất kỳ tế bào khá bạn có thể di chuyển lên, xuống, trái và phải, nếu các tế bào tương ứng với phong trào tồn tại và là mức khá. Nhiệm vụ của bạn là xây dựng một con đường bên trong một số lượng tối thiểu của các tế bào, do đó, nó sẽ có thể để có được từ tế bào bất kỳ tiểu bang nào với bất kỳ tế bào của bất kỳ nhà nước khác bằng cách sử dụng các tế bào chỉ ở mức khá. Nó được đảm bảo rằng ban đầu nó có thể đạt được bất kỳ tế bào của bất kỳ nhà nước từ bất kỳ tế bào của tiểu bang này, di chuyển chỉ dọc theo tế bào của nó. Nó cũng được đảm bảo rằng đối với bất kỳ nhà nước có ít nhất một tế bào thuộc về nó. Input Dòng đầu tiên của đầu vào có chứa các kích thước của n bản đồ và m (1 ≤ n, m ≤ 1000) - số lượng hàng và cột tương ứng. Mỗi phòng trong số n dòng tiếp theo chứa các ký tự m, mô tả các hàng của bản đồ. Chữ số 1-3 đại diện cho các phụ kiện cho nhà nước tương ứng. Nhân vật '.' tương ứng với các tế bào, nơi nó được cho phép để xây dựng một con đường và các ký tự '#' có nghĩa là không xây dựng được cho phép trong các tế bào này. Output In một số nguyên duy nhất - số lượng tối thiểu của các tế bào bạn cần xây dựng một con đường bên trong để kết nối tất cả các tế bào của tất cả các nước. Nếu một mục tiêu như vậy là không thể thực hiện, in ra -1. Kiểm tra mẫu (s)
đang được dịch, vui lòng đợi..